sanic - 如何指定所需的 JSON?

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

我想为控制器指定所需的 JSON

get
/
post
方法,以便它在 SwaggerUI 中显示。

例如,我希望

request.json
看起来像这样:

{
  'key1': <int>,
  'key2': <string>
}

我像这样初始化 SwaggerUI:

from sanic_openapi import swagger_blueprint, openapi_blueprint

app = Sanic(__name__)

# Set up SwaggerUI
app.blueprint(openapi_blueprint)
app.blueprint(swagger_blueprint)

如何让两个键都显示在

parameters
中?

python python-3.5 sanic
3个回答
1
投票

sanic_openapi.doc.consumes
装饰器用于装饰视图函数以记录其输入。装饰器函数的命名遵循 OpenAPI 规范

这是一种应用方法:

@app.post('/recording_test')
@doc.summary('Tests a recording')
@doc.consumes({'key1': str, 'key2': int}, location='body')
async def create_recording_test(request):
    ...

您可以使用类对输入进行建模。

class RecordingTest:
    key1 = str
    key2 = int

按以下方式使用上面的建模输入

@app.post('/recording_test')
@doc.summary('Tests a recording')
@doc.consumes(RecordingTest, location='body')
async def create_recording_test(request):
    ...

0
投票

您没有在 swagger 中看到这个参数,因为您的 get 方法在类中。 Sanic-openapi 和 Sanic-swagger 尚不支持基于类的视图。 :(


0
投票

如果您正在阅读本文并想知道 @doc 和 sanic-openapi 发生了什么,Sanic 扩展接管了,并且 sanic-openapi 现已弃用。

你可以这样完成同样的事情:

from sanic_ext import openapi
@app.route("/endpoint_printrrr")
@openapi.summary('Endpoint goes brrr')
@openapi.parameter(parameter=Parameter("issue_id", schema=str, deprecated=False))
async def endpoint_printrrr(request: Request) -> JSONResponse:
    ...codez

愿主上帝祝福并保守你在基督里 - 达拉克

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