'How to manually customize parameters of DRF views using drf-yasg Swagger?

I am using drf-yasg package to integrate Swagger with DRF.

As documentation said I used @swagger_auto_schema decorator to manually customize auto-generated endpoints. After a lot of tries I still can't figure out why there are no any changes.

So, I tried to add extra query parameter to RetrieveUpdateAPIView:

class MyCustomView(RetrieveUpdateAPIView):
    ...

    @swagger_auto_schema(
        manual_parameters=[openapi.Parameter('test', openapi.IN_QUERY, description="test manual param", type=openapi.TYPE_BOOLEAN)]
    )
    def retrieve(self, request, *args, **kwargs):
        ...

After all, nothing seems changed. What exactly I have to do then?



Solution 1:[1]

you have to add swagger_auto_schema in get method instead of retrieve.

@swagger_auto_schema(
        manual_parameters=[openapi.Parameter('test', openapi.IN_QUERY, description="test manual param", type=openapi.TYPE_BOOLEAN)]
    )
    def get(self, request, *args, **kwargs):
        ...

Solution 2:[2]

A query_serializer parameter was added in 1.18 (https://github.com/axnsan12/drf-yasg/pull/17).

Example:

    from rest_framework import serializers
    from rest_framework import viewsets
    from drf_yasg.utils import swagger_auto_schema
    
    class CustomParametersSerializer(serializers.Serializer):
        myparam = serializers.CharField(help_text="My manual querystring parameter")

    class MyViewSet(viewsets.ViewSet):
        @swagger_auto_schema(query_serializer=CustomParametersSerializer)
        def my_route(self, request):
            ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RAJESH SAHA
Solution 2 bubbassauro