在Swagger UI中显示响应架构的特定值

时间:2020-07-04 05:10:41

标签: python swagger swagger-ui marshmallow

我正在为可以使用200或40x HTTP代码以及状态字段的JSON响应的方法开发模式描述。我想使用不同响应的正确值使状态字段可见。我尝试了两种方法:1.使用fields.Str和2.使用fields.Constant

from marshmallow import Schema, fields

class SchemaSuccess(Schema):
    status = fields.Str(default='SUCCESS', description='Response status')
    status = fields.Constant('SUCCESS')

class SchemaError(Schema):
    status = fields.Str(default='ERROR', description='Response status')
    status = fields.Constant('ERROR')
    description = fields.Str(description='Error description')


from aiohttp import web
from aiohttp_apispec import docs
routes = web.RouteTableDef()

@routes.post('/my_api_method')
@docs(
    summary='Do smth with a phone number',
    description='Do smth with a phone number',
    responses={
        200: {
            'schema': SchemaSuccess,
            'description': 'SMS is sent',
        },
        400: {
            'schema': SchemaError,
            'description': 'Bad request',
        },
    }
)
async def my_api_method(request):
    ...

1。 fields.Str

该值传播到JSON API规范:

{
    "SchemaSuccess": {
        "type": "object",
        "properties": {
            "status": {
                "type": "string",
                "description": "Response status"
            }
        }
    },
}

因此Swagger用户界面中未显示任何值:

fields.Str in the Swagger UI

2。 fields.Constant

被传播到JSON API规范:

{
    "SchemaSuccess": {
        "type": "object",
        "properties": {
            "status": {
                "default": "SUCCESS"
            }
        }
    },
}

但是该字段在Swagger UI中根本没有显示:

fields.Constant in the Swagger UI


那么,如何在Swagger UI中显示响应模式的特定值?

我使用Python 3,marshmallowaiohttp-apispecapispecswagger-ui

0 个答案:

没有答案