I've overridden the list function from django rest viewset to customize the response body (it returned an json array, I wanted to return an object that contains the array) I want to put this response in swagger doc too! I use drf_yasg app in django. I've written this code:
from drf_yasg.openapi import Schema, TYPE_OBJECT, TYPE_STRING, TYPE_ARRAY
from drf_yasg.utils import swagger_auto_schema
class StudentViewSet(viewsets.ModelViewSet):
@swagger_auto_schema(responses={200: Schema(type=TYPE_OBJECT)})
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response({'students': serializer.data})
I don't know how to give my response object description to the Schema object. I've searched so much but I found nothing useful!
Try the following code:
@swagger_auto_schema(responses={
status.HTTP_200_OK: Schema(
type=TYPE_OBJECT,
properties={
'students': Schema(
type=TYPE_ARRAY
)
}
)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With