Sanic OpenAPI Swagger文档生成抛出属性路径operationId重复

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sanic OpenAPI Swagger文档生成抛出属性路径operationId重复相关的知识,希望对你有一定的参考价值。

我正在使用sanic构建简单的API。我定义了两个端点:

from sanic import Blueprint, response
from sanic_openapi import doc

bp = Blueprint('default', url_prefix="", version=2, strict_slashes=True)


@bp.get("")
@doc.summary('Default GET endpoint for API v2')
async def route_get(request):
    resp = {"message": "New API - GET", "version": 2.0}
    return response.json(resp)


@bp.post("")
@doc.summary('Default POST endpoint for API v2')
async def route_post(request):
    resp = {"message": "New API - POST", "version": 2.0}
    return response.json(resp)

生成的草签文档验证失败,并显示以下消息

错误:

"attribute paths.'/v2/'(post).operationId is repeated",

我期望有多个路由,这些路由具有多个前往相同路径的HTTP动词:

GET /v2/product
POST /v2/product 

DELETE /v2/product/{id}
PUT /v2/product/{id}

我也已经使用这些端点进行了测试,关于重复的operationId,我会收到两个错误。一种用于/v2/product路径,另一种用于/v2/product/{id}

我该如何解决此错误?

答案

类似于下面的代码可以提供帮助,因此您可以为POST,GET,PUT,DELETE分别使用相同的路径,并编写自己的逻辑。

我对您的代码做了一些修改,下面是代码:

from sanic import Blueprint, response
from sanic_openapi import doc

bp = Blueprint('default', url_prefix="", version=2, strict_slashes=True)

@bp.get("/<product_id:int>", strict_slashes=True)
@doc.summary('Default GET endpoint for API v2')
async def route_get(request):
    resp = {"message": "New API - GET", "version": 2.0}
    return response.json(resp)


@bp.put("/<product_id:int>", strict_slashes=True)
@doc.summary('Default POST endpoint for API v2')
async def route_post(request):
    resp = {"message": "New API - POST", "version": 2.0}
    return response.json(resp)

如果必须将product与模型对象一起使用,则可以使用类似下面的内容,我已经创建了完整的示例供您参考。

文件夹结构:

 Project
    blueprints
        product.py
    data.py
    main.py
    models.py

product.py

from sanic.blueprints import Blueprint
from sanic.response import json
from sanic_openapi import doc

from models import Product
from data import test_product


blueprint = Blueprint('Products', '/products')

@blueprint.get("/<product_id:int>", strict_slashes=True)
@doc.summary("Fetches a product with product Id")
def route_get(request, product_id):
    resp = {"message": "New API - GET", "version": 2.0}
    return json(resp)


@blueprint.put("/<product_id:int>", strict_slashes=True)
@doc.summary("Updates a Product with the Updated product Details from the contends from Body")
@doc.consumes(Product, location='body')
def route_post(request, product_id):
    resp = {"message": "New API - POST", "version": 2.0}
    return json(resp)

data.py

from models import Product
import datetime

test_product = Product()

test_product = {
    'id': 1,
    'name': 'Gross'
}

main.py

从sanic进口Sanic

from sanic_openapi import swagger_blueprint
from blueprints.product import blueprint as product_blueprint

app = Sanic()

app.blueprint(product_blueprint)

app.config.API_VERSION = '1.0.0'
app.config.API_TITLE = 'Product API'

app.run(host="0.0.0.0", debug=True)

models.py

from sanic_openapi import doc

class Product:
    id = int
    name = str

正在运行python main.py

控制台输出:

[2019-11-22 16:20:53 +0530] [15744] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2019-11-22 16:20:53 +0530] [15744] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-11-22 16:20:53 +0530] [15744] [WARNING] Sanic tried to use loop.add_signal_handler but it is not implemented on this platform.
[2019-11-22 16:20:53 +0530] [15744] [INFO] Starting worker [15744]

以上是关于Sanic OpenAPI Swagger文档生成抛出属性路径operationId重复的主要内容,如果未能解决你的问题,请参考以下文章

Swagger生成接口文档

基于NSwag.CodeGeneration.CSharp生成Swagger/OpenAPI客户端C#代码

在 PHP 中自动生成 openAPI(swagger) 规范文件

.NET Core基础篇之:集成Swagger文档与自定义Swagger UI

如何使用Swagger-UI在线生成漂亮的接口文档

Spring Boot - 自动生成接口文档