我正在为可以使用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):
...
fields.Str
该值未传播到JSON API规范:
{
"SchemaSuccess": {
"type": "object",
"properties": {
"status": {
"type": "string",
"description": "Response status"
}
}
},
}
因此Swagger用户界面中未显示任何值:
fields.Constant
值 被传播到JSON API规范:
{
"SchemaSuccess": {
"type": "object",
"properties": {
"status": {
"default": "SUCCESS"
}
}
},
}
但是该字段在Swagger UI中根本没有显示:
那么,如何在Swagger UI中显示响应模式的特定值?
我使用Python 3,marshmallow
,aiohttp-apispec
,apispec
和swagger-ui
。