-
Notifications
You must be signed in to change notification settings - Fork 265
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
drf-spectacular automatically paginates error responses #277
Comments
i see. that is the trade-off for modeling errors with "normal" responses. you can get around this with: class XViewset(viewsets.ViewSet):
queryset = SimpleModel.objects.all()
serializer_class = SimpleSerializer
pagination_class = pagination.LimitOffsetPagination
@extend_schema(
responses={
200: SimpleSerializer,
400: extend_schema_serializer(
many=False,
examples=[OpenApiExample('Invalid Request', value={"detail": "Malformed request."}, status_codes=['400'])]
)(
inline_serializer('Error500', {'detail': serializers.CharField()})
)
},
)
def list(self, request, *args, **kwargs):
pass this is just a compact example. important part is i can see that this is not pretty, but im still thinking about your suggestion regarding raw dict and +300 codes. it makes sense, but also breaks consistency and might be unexpected. |
@tfranzel what about situations where I have a pagination class set on a ModelViewSet but I want to disable it for the list action? @extend_schema(summary='Get All Groups', description="Get all of the user's groups")
def list(self, request, *args, **kwargs):
# overriding the method here to disable pagination
queryset = self.filter_queryset(self.get_queryset())
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data) in this case, I can pass to the decorator the responses serializer and pass many=False to the serializer but then the generated schema will show only one object and I do want to keep the list. class GroupSerializer(serializers.ModelSerializer):
# My serializer stuff
@extend_schema_serializer(many=False)
class GroupsListReturn(serializers.Serializer):
groups = GroupSerializer(many=True)
class Meta:
fields = ('groups',) This code is able to disable the pagination from the response but changes the response from:
to:
|
@yovelcohen to what end? |
@tfranzel |
i see your problem now and it is tricky. i agree that we need to change something to accommodate that. pagination/list detection is an example where we have to use heuristics because it's not 100% certain what the expected outcome should be. in principle pagination is only specified for
yes we use that. if you customize the pagination class there, it will get used. customizing the the "pagination form" is possible there, but its not possible the the fix your root issue with that. |
@yovelcohen in any case, this is the way i handled this before. i think this is quite clear and you don't use hacks in spectacular, just the regular DRF action configuration ( class XViewset(viewsets.ReadOnlyModelViewSet):
queryset = SimpleModel.objects.all()
serializer_class = SimpleSerializer
pagination_class = pagination.LimitOffsetPagination
@extend_schema(responses={'200': SimpleSerializer(many=True)})
@action(methods=['GET'], detail=False, pagination_class=None)
def custom_action(self):
pass # pragma: no cover |
although it is a deviation from the main behaviour, paginating error responses makes little sense in virtually all realistic scenarios.
this has been bugging me for a while now. @szymonhab i changed the behavior to what you would have expected in your initial post. after all it makes the most sense, even if it introduces status code dependent processing. at the end of the day, paginated i would argue it now follows the principle of least surprise. |
closing this issue for now. feel free to comment if anything is missing or not working and we will follow-up. |
Describe the bug
When there is DEFAULT_PAGINATION_CLASS specified in DRF settings or view implements pagination drf-spectacular is automatically adding pagination also to not successful list responses.
Expected behavior
I think that for responses other than 2** pagination shouldn't be included. Though it might be too much "magical" so maybe some additional variable in @extend_schema would do a job?
There is one more way to solve it: don't wrap response with pagination if response is raw dict.
Please correct me if there is ANY way to document such endpoint at this moment.
The text was updated successfully, but these errors were encountered: