djangodjango-rest-frameworkswaggerdrf-yasg

Manually Adding Swagger Documentation Does Not Show Example Values in Responses Section


I have a simple django rest API project with some endpoints. I have installed swagger API documentation for viewing and testing APIs. as you know the APIs that are dependent on models and serializers will be added to the swagger documentation automatically and there is no problem with them. I have another view which is not related to a model or serializer so I wrote the swagger code for it manually. now the problem I faced is that the example values of the Responses section in this endpoint are not loaded and a circle is just turning! here is the code and the image of swagger:

import requests
from django.shortcuts import render
from store.models import Customer
from .serializers import CustomerSerializer
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema


class Verification(APIView):
    @swagger_auto_schema(
        request_body=openapi.Schema(
            type=openapi.TYPE_OBJECT,
            properties={
                'phone': openapi.Schema(type=openapi.TYPE_STRING, description='Phone number to verify'),
                'token': openapi.Schema(type=openapi.TYPE_STRING, description='Verification token sent via SMS')
            },
            required=['phone', 'token']
        ),
        responses={
            200: openapi.Response(
                description='Successful verification',
                examples={
                    'application/json': {
                        'status': True,
                        'detail': '200, your entered token matched.',
                    }
                }
            ),
            400: openapi.Response(
                description='Invalid input or verification failed',
                examples={
                    'application/json': {
                        'status': False,
                        'detail': 'entered token is NOT true',
                    }
                }
            )
        }
    )
    def post(self, request):
        number = request.data.get('phone')
        sent_tok = request.data.get('token')
        if number and sent_tok:
            old = Customer.objects.filter(phone__iexact=number)
            if old.exists():
                old = old.first()
                saved_tok = old.sms_token
                if str(sent_tok) == str(saved_tok):
                    old.is_verified = True
                    old.save()
                    return Response({
                        'status': True,
                        'detail': '200, your entered token matched.'
                    })
                else:
                    return Response({
                        'status': False,
                        'detail': 'entered token is NOT true'
                        })

example values aren't loaded

I asked AI and searched but it seems the problem is not from code. I have also installed the last version of swagger by pip.


Solution

  • You need to click on that 'Example value' in order to show it.

    enter image description here