-
Notifications
You must be signed in to change notification settings - Fork 301
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
Pass on instance when using polymorphic serializers #764
Conversation
Codecov Report
@@ 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.
|
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
There was a problem hiding this 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
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 |
There was a problem hiding this 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).
…nstance to the child serializer
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
6cdf8fb
to
0ef5688
Compare
There was a problem hiding this 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.
There was a problem hiding this 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.
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 ofinstance
anddata
, which can fix potential issues when extending serilaizers that are part of a polymorphic serializer.Checklist
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 futuredocumentation 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-> This is not a user relevant changeCHANGELOG.md
updated (only for user relevant changes)AUTHORS
-> It already was there