I have a class based view as:

class ClassBasedView(GenericAPIView):
    
    @swagger_auto_schema(responses={201: 'Created'})
    @authorize(myCustomPermission)
    def post(self, *args, **kwargs) -> Response:
        // code.....
        return Response(status=HTTPStatus.CREATED)

First:

The use of swagger_auto_schema without any serializer is throwing error as: AssertionError: DetailView should either include a serializer_class attribute, or override the get_serializer_class() method.

And I don't want to use serializer for this endpoint as I don't need that. But the swagger_auto_schema keeps on throwing this error. I want to know whether there is any way to avoid the use of serializer and get the swagger documentation of this endpoint.

Second:

I want to add my custom authorisation permission of this endpoint in the doc. There is a security field in swagger_auto_schema, but don't know how to make it to use for my custom permission class ie myCustomPermission

Thanks.

1

There are 1 best solutions below

0
On
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema

ClassBasedView_request_body = openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'domain': openapi.Schema(type=openapi.TYPE_STRING),
        'subdomain': openapi.Schema(type=openapi.TYPE_STRING),
        'code': openapi.Schema(type=openapi.TYPE_INTEGER),
        'status': openapi.Schema(type=openapi.TYPE_BOOLEAN)
    },
    required=['domain', 'subdomain', 'code']
) 
ClassBasedView_200response = openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'Success': openapi.Schema(type=openapi.TYPE_BOOLEAN),
        'Message': openapi.Schema(type=openapi.TYPE_STRING)  
    }
)    
ClassBasedView_400response = openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'Success': openapi.Schema(type=openapi.TYPE_BOOLEAN, default=False),
        'Error': openapi.Schema(type=openapi.TYPE_STRING)
    },
)    
ClassBasedView_response = {
    '200': ClassBasedView_200response,
    '400': ClassBasedView_400response,
}

class ClassBasedView(GenericAPIView):

    @swagger_auto_schema(request_body=ClassBasedView_request_body, responses=ClassBasedView_response)
    def post(self, request, *args, **kwargs):
        try:
            pass
            #your code....
        except Exception as err:
            return Response({'Success': False, 'Error': err}, status=400)

For your first question this code might help you.