4

如何使用drf-yasg包将示例响应(openapi doc)添加到我的 swagger 文档中?

4

1 回答 1

9

drf_yasg.openapi.Response在-- (drf-yasg doc) 装饰器的帮助下使用-- @swagger_auto_schema(...)(drf-yasg doc)作为

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from rest_framework.response import Response
from rest_framework.views import APIView

response_schema_dict = {
    "200": openapi.Response(
        description="custom 200 description",
        examples={
            "application/json": {
                "200_key1": "200_value_1",
                "200_key2": "200_value_2",
            }
        }
    ),
    "205": openapi.Response(
        description="custom 205 description",
        examples={
            "application/json": {
                "205_key1": "205_value_1",
                "205_key2": "205_value_2",
            }
        }
    ),
}


class MyTestAPIView(APIView):

    @swagger_auto_schema(responses=response_schema_dict)
    def post(self, request, *args, **kwargs):
        return Response({"foo": "bar"})

Schema 渲染结果

Schema 渲染结果

更新

它继续加载并且不显示任何内容

如果您正在查看Swagger文档,您可能需要单击“示例值”文本

加载微调器

于 2020-09-29T03:56:49.787 回答