-
Notifications
You must be signed in to change notification settings - Fork 270
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
Consider the renderer class when generating the schema #49
Comments
that is an awesomely detailed issue 👍
that is correct. there is no (reasonable) way of introspecting the magic in the
that might be a lucky accident... its a 50/50 thing
so far so good.
that does not look like fun 😄
does that not work without the i believe i found the problem. it is with |
It works if there is method to decorate in declared class (not inherited from base class). This code was copied from viewset without one.
Well, DRF has found a way around the same problem with pagination using |
the idea in the beginning was that this is also supposed to work for @extend_schema(..., methods=['POST'])
class XViewset(mixins.CreateModelMixin,GenericViewSet):
serializer_class = foo
queryset = bar but it breaks atm on the
yes, they found a way there and we already use it in spectacular. would be possible for renderer classes too, but in theory the renderer is supposed to be transparent. at least that is my understanding. i would cautiously guess that this would have only a minor chance of getting merged. @MissiaL: all the wrapping and all stuff that is going on there does not matter i think. its just that you try to massage an explicit drf-spectacular/drf_spectacular/openapi.py Lines 99 to 100 in 97ce18e
|
What do you mean by that? Seems like renderer is default place to put envelopes around data, because other formats like XML will make their own envelope.
I think that real problem here is that we pass already enveloped serializer to |
you make a good point there. i could be totally wrong about this. I just have not encountered DRF renderer modifications like this. though i seen this outside of python before. would be awesome if anyone volunteers to do a PR at DRF for this. the def test_viweset_list_coerced_to_retrieve(no_warnings):
class XSerializer(serializers.Serializer):
x = serializers.IntegerField()
class XViewset(mixins.ListModelMixin, viewsets.GenericViewSet):
@extend_schema(responses={200: XSerializer()})
def list(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs)
schema = generate_schema('x', viewset=XViewset)
operation = schema['paths']['/x/']['get']
# FAILS
assert operation['responses']['200']['content']['application/json']['schema']['type'] == 'object' we had a similar problem in #20. it is still a heuristic. plus you cannot actively do also i have not found out whether yasg has this right by accident or it's just a glitch, because their heuristic is very similar. would be happy for any suggestions here. |
What if you use the new parameter in Example: class ResponsePaginator(LimitOffsetPagination ):
def get_paginated_response_schema(self, schema):
return schema['items']
@extend_schema(responses={200: XSerializer()}, response_paginator=ResponsePaginator)
def list(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs) |
i see what you getting at there. is see 3 the problem with that
|
I agree with you. This is not explicit 😔 Maybe we can add some other parameter that would change the scheme for receiving a response? |
I see two approaches here: make heuristics better or give a switch to disable them (because right now they break our scheme and give no way to make it right). |
i think the heuristic cannot be improved here. its simply missing one bit of information. in theory serializers would need 3 init states:
but only state 1. and 3. exist. the missing 2. is not achievable due to DRF serializer mechanics. i will come up with something. probably another decorator. here, you would use it like this |
b814fa2 should at least get rid of that ugly three param |
added mechanics for explicit serializer defaults override. wasn't all that bad. caveat: pagination will not work anymore on those endpoints. that would need to be manually handled then. i guess you can't have you cake AND eat it after all. this is how i achieved your drf-spectacular/tests/test_regressions.py Lines 502 to 513 in df22119
|
It works perfect! Thanks! |
sure thing. please test the versioning #22 and let me know. i'd like to include both things into the next release. |
Describe the bug
When generating a scheme, the renderer_classes is not used. This leads to a problem when the serializer is wrapped in an additional list. We do not use pagination in the viewset.
To Reproduce
Our example
After that we got schema:
Expected behavior
We need to get the right scheme without wrapped list
I also want to say that the
drf_yasg
package correctly processes this situation. Maybe you can see the implementationThe text was updated successfully, but these errors were encountered: