Fix context being ignored in GenericAPIView.get_serializer#7162
Fix context being ignored in GenericAPIView.get_serializer#7162mrtaalebi wants to merge 5 commits into
Conversation
Fix GenericAPIView.get_serializer(self, *args, **kwargs) ignores context in kwargs and replaces it (which may be used by views) with its default context
|
I don't get why there should be some extra context passed as argument to |
|
Well, I have two reasons:
|
drsantos20
left a comment
There was a problem hiding this comment.
Hi @mrtaalebi do we have any tests for the new condition?
rpkilby
left a comment
There was a problem hiding this comment.
I'm -1 to this change as-is (mixing context from the default with the values passed via kwargs is more confusing to me than not).
That, said, I can understand that it's confusing that the context passed to get_serializer is overwritten by the result of get_serializer_context. We should either do
kwargs.setdefault('context', self.get_serializer_context())Or, we should raise an error/warning notifying the user that the supplied context is overwritten.
Raise and AssertionError whenever a `context` argument is passed to the `get_serializer`
|
Hi @rpkilby,
So... I yield! But your suggestion about raising an error is great. I've committed the new changes |
|
Hi @drsantos20 I am convinced that my previous change could make some problems whenever a developer wants to have And about the last change ( |
|
Thanks. Adding to the milestone for review. |
|
I would suggest the following: kwargs.setdefault("context", {}).update(self.get_serializer_context()) |
| "`get_serializer` does not accept a `context` argument, " | ||
| "you may override `get_serializer_context` instead." | ||
| ) | ||
| kwargs['context'] = self.get_serializer_context() |
There was a problem hiding this comment.
I really think we ought to use...
if 'context' not in kwargs:
...I don't think we need to deal with "merge the context if one is passed" - if someone's passing an explicit context to the get_serializer method, then it's up to them to include any keys as appropriate.
There was a problem hiding this comment.
Hey Tom!
What you're saying is what came to my mind in the first place and seemed to be very reasonable that I made these changes (and of course fixed typos later in the next commit :P).
But as @rpkilby commented that he's -1 to this change above, I've decided to follow him and changed it to be at least less confusing to anyone facing the behavior (context gets vanished).
So whatever you say! I can keep the last commit or revert it.
|
Superseeded by #7298 |
Fix GenericAPIView.get_serializer(self, *args, **kwargs) ignores context in kwargs and replaces it (which may be used by views) with its default context