-
Notifications
You must be signed in to change notification settings - Fork 301
Allow overwriting of get_serializer_class with related urls #859
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
Comments
This issue only seems to relate to related urls which need to be specially configured to enable. Otherwise it should not be a problem to overwrite I think the issue is that PR is welcome. |
If I understand you correctly, the actual bug is that Then, a fix might lead to some upstream user code to break that is already designed around the bug. |
An attempt to fix django-json-api#859, which unfortunately breaks two tests in test_views.
Here is my attempted fix along the lines outlined above. Unfortunately, it does not seem to work, as it breaks two assertions in |
After some more debugging, I didn't get any further unfortunately. It seems that |
To clarify, this particular issue only affects users who use related urls. Such are not on by default but need to be activated through a specific url pattern as outlined in the documentation. Without those urls this if statement won't be true and the I haven't analyzed the issue fully but I assume the issue lies here where the resource name is determined. Most likely does it need a similar if as here to determine whether it needs to get |
Thanks for this additional hint. Your assumption seems to be right. I added an if-statement to determine if the resource name of the related resource is needed or the parent resource. I extended the Author view in the example to test the issue - made sure that it fails on master first, then add my changes and see that the tests pass. This is where another issue now pops up, in OpenAPI generation: |
I do not quite see how such a change makes those tests fail. If you don't find the issue feel free to create a PR with what you got. |
The problem seems to be in the snapshot tests, where the expected response does not match anymore. Instead of "items": {
"$ref": "#/components/schemas/Author"
}, we now get "items": {
"$ref": "#/components/schemas/AuthorList"
}, I suppose the new expected result is correct, but I can't tell for sure because I am not familiar with the OpenAPI schema generation. I will open a PR of my changes, so you can judge for yourself. Thanks for your help! |
An attempt to fix django-json-api#859, which unfortunately breaks two tests in test_views.
See my review comments. |
…rls (#860) * This attempt to fix the issues breaks tests An attempt to fix #859, which unfortunately breaks two tests in test_views. * Removed misleading comment Tha basic method was copied over from the GenericAPIView. The comment here does not fit, though. * bein more explicit about the changes * Use correct serializer for rendering Ensure that the correct resource is returned during rendering, which in turn requires to use the correct serializer, depending on if it is a related resource or the parent resource. sliverc pointed out the issue here. * Add provision for tests * Describe the fix. * Fixed failing tests As pointed out by n2ygk, the schema names here must reflect the Serializer class names, which I changed in the two affected test cases. * Update CHANGELOG.md Clarified changelog entry * Fixed linting issues Co-authored-by: Ulrich Schuster <[email protected]> Co-authored-by: Oliver Sauder <[email protected]>
In
django-rest-framework-json-api/rest_framework_json_api/views.py
Line 182 in a67a521
the serializer class defined in the view is used directly. This is a problem if one wants to override
get_serializer_class()
; e.g., to provide a different serializer depending on the view action. Is there a reason to prevent dynamic serializer selection here viaget_serializer_class()
?The text was updated successfully, but these errors were encountered: