drf_yasg documentation parameters not showing

7.8k Views Asked by At

I am using drf_yasg to self document my API which is built using the django-framework, but none of the parameters are displaying at all for any of the endpoints. The way I've done it is to pass the parameters within the http request body in JSON format. I then read the parameters from request.data. Below is an example of one of the views where it takes 'id' as a parameter in the body of the request.

@api_view(['POST'])
@permission_classes([IsAuthenticated])
def get_availability_by_schoolid(request):
    if request.user.is_donor:
        try:
            #Get the existing slots and remove them to replace with new slots
            slots = SchoolSlot.objects.filter(school__id=request.data["id"]).values('day','am','pm')
            availability = process_availability(slots)
            availabilityslotserializer = SchoolSlotSerializer(availability)
            return Response(availabilityslotserializer.data, status=status.HTTP_200_OK)
        except Exception as e:
            print(e)
            return Response(ResponseSerializer(GeneralResponse(False,"Unable to locate school")).data, status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(ResponseSerializer(GeneralResponse(False,"Not registered as a donor")).data, status=status.HTTP_400_BAD_REQUEST)

This will display in the swagger documentation as below:

swagger output

I have been looking at the documentation and it suggests that i might need to add a decorator with manual parameters, but I can't figure out how I would do that as the examples I can find are for get query parameters or path parameters. I have added the decorator below which enables the response to document.

@swagger_auto_schema(method='post', responses={200: SchoolSlotSerializer,400: 'Bad Request'})

An example of the request body for this is:

  { "id": 43 }

UPDATE:

I seem to get something with the following decorator:

@swagger_auto_schema(method='post', request_body=openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'id': openapi.Schema(type=openapi.TYPE_INTEGER, description='Donor ID')
    }),
    responses={200: SchoolSlotSerializer,400: 'Bad Request'})

This documents the parameters. Now what I have for another endpoint is it takes the following JSON for input, how would I go about adding this?

  {
        "availability": {
            "mon": {
                "AM": true,
                "PM": false
            },
            "tue": {
                "AM": false,
                "PM": false
            },
            "wed": {
                "AM": false,
                "PM": false
            },
            "thu": {
                "AM": true,
                "PM": true
            },
            "fri": {
                "AM": false,
                "PM": false
            },
            "sat": {
                "AM": false,
                "PM": false
            },
            "sun": {
                "AM": true,
                "PM": false
            }
    }
    }
3

There are 3 best solutions below

1
On

you can use your ModelSerializer for that:

@swagger_auto_schema(method='post', request_body=ModelSerializer)

0
On

I believe this is what enabled it to work:

If there is a better way then please let me know.

@swagger_auto_schema(method='post', request_body=openapi.Schema(
    type=openapi.TYPE_OBJECT,
    properties={
        'id': openapi.Schema(type=openapi.TYPE_INTEGER, description='Donor ID')
    }),
    responses={200: SchoolSlotSerializer,400: 'Bad Request'})
0
On

If you are using class based view, do something like this:

from drf_yasg.utils import swagger_auto_schema

class EmployeeApiView(APIView):

 @swagger_auto_schema(request_body=EmployeeSerializer)
 def post(self, request, format=None):

    ...