URL querystring not parsed correctly in 0.7.0
soulshake opened this issue · 4 comments
It seems that since upgrading to 0.7.0, drf-flex-fields no longer parses expansions requested via URL querystring the same way.
I have been doing this:
class EphemeronViewSet(FlexFieldsModelViewSet):
[...]
def get_serializer(self, *args, **kwargs):
if "expand" in self.request.query_params: # add from querystring in URL
kwargs["expand"] = self.request.query_params['expand']
return super(EphemeronViewSet, self).get_serializer(*args, **kwargs)
which allows expanding fields like this:
curl '0.0.0.0:8000/api/ephemera/?expand=user'
But since upgrading to 0.7.0, the field no longer gets expanded in the output.
I think these changed lines are what resulted in the change in behavior. If I add a print(passed)
just after that and compare previous behavior to 0.7.0:
Before:
{'expand': 'user', 'fields': [], 'omit': []}
After:
{'expand': ['u', 's', 'e', 'r'], 'fields': [], 'omit': []}
Perhaps these lines should be something more like [kwargs.pop("expand")] if "expand" in kwargs else []
instead of list(kwargs.pop("fields", []))
?
(Or perhaps there's a better way to pass querystrings to drf-flex-fields than what I've been doing in my get_serializer()
override as shown above? I have been doing it that way in order to expand fields in response to a POST request, if I recall recorrectly.)
Thanks, I'm looking into this.
I appreciate the detailed issue. Yes, I agree that those changes lines are responsible. Before, it seems like things worked when you passed a string value for expand when constructing a serializer:
serializer = EphemeronSerializer(object, expand='user')
As I understand from your issue, this used to work. However, I did not intend to support passing strings to the keyword serializer options (expand, fields, omit). Ensuring that you pass a list for the expand
value should fix this for you.
serializer = EphemeronSerializer(object, expand=["user"])
But ... what you are doing in the get_serializer
method may not be necessary because this package will automatically read the expand value from request's query params (if it is not passed explicitly as a kwarg to the constructor):
I see! I'll look into other ways of expanding fields in response to POST requests. I see it was discussed on #25.
Thanks, feel free to close!
Ah I see. Yes, I should revisit that issue.