手动添加 Swagger 文档不会在响应部分显示示例值

问题描述 投票:0回答:1

我有一个带有一些端点的简单 django Rest API 项目。我已经安装了 swagger API 文档来查看和测试 API。如您所知,依赖于模型和序列化器的 API 将自动添加到 swagger 文档中,并且它们没有问题。我有另一个与模型或序列化器无关的视图,因此我手动为其编写了 swagger 代码。现在我面临的问题是该端点中响应部分的示例值未加载,并且圆刚刚转动! 这是代码和 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'
                        })

示例值未加载

我问了AI并进行了搜索,但似乎问题不是来自代码。 我还通过 pip 安装了最新版本的 swagger。

django django-rest-framework swagger drf-yasg
1个回答
0
投票

您需要单击“示例值”才能显示它。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.