Skip to content

Pass on instance when using polymorphic serializers #764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

josebama
Copy link
Contributor

@josebama josebama commented Jan 22, 2020

Fixes #759

Description of the Change

As per issue #759 pass self.instance as the first parameter when initialising the child serializer from a polymorphic serialiser. This does not affect basic functionality, but makes the child serializer instance consistent with its usage of instance and data, which can fix potential issues when extending serilaizers that are part of a polymorphic serializer.

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added -> I couldn't find anywhere relevant to add such a test without making the code less maintainable and harder to refactor in the future
  • documentation updated -> Being such a minimal change that doesn't affect normal usage of the library, I think it would complicate things more than anything else
  • CHANGELOG.md updated (only for user relevant changes) -> This is not a user relevant change
  • author name in AUTHORS -> It already was there

@codecov
Copy link

codecov bot commented Jan 22, 2020

Codecov Report

Merging #764 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #764   +/-   ##
=======================================
  Coverage   97.12%   97.12%           
=======================================
  Files          54       54           
  Lines        2779     2779           
=======================================
  Hits         2699     2699           
  Misses         80       80

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e3ae420...b5e0d04. Read the comment docs.

@codecov
Copy link

codecov bot commented Jan 22, 2020

Codecov Report

Merging #764 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #764      +/-   ##
==========================================
+ Coverage   97.13%   97.15%   +0.02%     
==========================================
  Files          54       54              
  Lines        2795     2816      +21     
==========================================
+ Hits         2715     2736      +21     
  Misses         80       80
Impacted Files Coverage Δ
example/tests/test_serializers.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d5d7215...e3265de. Read the comment docs.

Copy link
Contributor

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should also add test for the proposed change

@josebama
Copy link
Contributor Author

I added a test. It's very hacky, but it's the only way I could think how to do it without making it too tied to the implementation

Copy link
Member

@sliverc sliverc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this. See inline comments.

And there also needs to be a changelog entry as a DJA user can certainly be affected by this when trying to access self.instance in a polymorphic child serializer. Today it is a dict, after this change it will be an object (as it should be).

Previously it was relying on the fact that `save` was later calling
 `to_internal_value`, which was then passing the instance to the children.
 Now if in the future `PolymorphicModelSerializer` initialises the children
 serializers at a different point, this test will remain relevant
Make the test more independent for the current DRF Serializer implementation by
overriding the `__init__` and asserting that it receives the expected arguments.

Also move the asserts to overridden init to avoid using extra unneeded variables
@josebama josebama force-pushed the bugfix/pass-instance-in-polymorphic-serializer branch from 6cdf8fb to 0ef5688 Compare January 27, 2020 09:48
@josebama josebama requested a review from sliverc January 29, 2020 11:41
Copy link
Member

@sliverc sliverc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your changes. One open issues remains otherwise it looks good.

The previous approach to restore the overridden init was not running if the
test failed, so it was only restoring the init when the test passed. By setting
it and restoring it during the setUp and tearDown of the test we ensure that it
will always be restored.

Also because the overridden init is now declared in the setUp, I have moved the
asserts back to the test for readability.
@josebama josebama requested a review from sliverc January 30, 2020 11:48
Copy link
Member

@sliverc sliverc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This LGTM now.

@sliverc sliverc merged commit 47fc77f into django-json-api:master Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pass on instance when using polymorphic serializers
3 participants