繁体   English   中英

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

[英]Sanic OpenAPI Swagger docs generation throws attribute paths operationId is repeated

我正在使用 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)

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

错误:

"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)

如果您必须将 Model Object 与产品一起使用,您可以使用类似下面的内容,我已经创建了一个完整的示例供您参考。

文件夹结构:

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

产品.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)

数据.py

from models import Product
import datetime

test_product = Product()

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

主文件

from sanic import 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)

模型.py

from sanic_openapi import doc

class Product:
    id = int
    name = str

现在运行python main.py

控制台 Output:

[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]

使用 API 访问 Swagger UI:

如果您必须使用基于 Class 的视图演示,以下是可以提供帮助的两个文件。

Project Structure
 Project
     main.py
     blueprint.py

主文件

from sanic_openapi import doc
from sanic_openapi import swagger_blueprint
from sanic import Sanic
from sanic.response import json

from blueprint import blueprint

app = Sanic()

app.blueprint(swagger_blueprint)
app.blueprint(blueprint)


@app.get("/product", strict_slashes=True)
@doc.summary("Displays the Product Details ")
async def get_product(request):
    return json({})

@app.post("/product", strict_slashes=True)
@doc.summary("Creates a product in Repositary")
@doc.consumes({"product": {"name": str}}, location="body")
async def create_product(request):
    return json({})


app.config.API_VERSION = 'pre-alpha'
app.config.API_TITLE = 'Display Products Demonstration API'

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

蓝图.py

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

blueprint = Blueprint('Class-based View', url_prefix='/class-based-view')
class MyView(HTTPMethodView):
    @doc.summary("Get my view")
    def get(self, request):
        return json({"method": "GET"})

    @doc.summary("Post my view")
    @doc.consumes({"view": {"name": str}}, location="body")
    def post(self, request):
        return json({"method": "POST"})


blueprint.add_route(MyView.as_view(), '/view', strict_slashes=True)

现在运行python main.py

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

您现在可以在http://localhost:8000/swagger/看到 SWAGGER UI

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM