简体   繁体   English

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

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

I am using sanic to build a simple API.我正在使用 sanic 构建一个简单的 API。 I have two end points defined:我定义了两个端点:

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)

The generated swagger documents fails validation, with the following message生成的 swagger 文档验证失败,并显示以下消息

Error:错误:

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

I am expecting to have several routes that have multiple HTTP verbs going to the same path:我期望有几条路线有多个 HTTP 动词进入同一路径:

GET /v2/product
POST /v2/product 

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

I've tested with these endpoints too, and I get two errors about operationId being repeated.我也用这些端点进行了测试,我得到了两个关于 operationId 重复的错误。 One is for the /v2/product path and one is for /v2/product/{id}一个用于/v2/product路径,一个用于/v2/product/{id}

How can I solve this error?我该如何解决这个错误?

Something like the below code can help, So that you can use the same path for POST,GET,PUT,DELETE Separately and write your own logic.类似下面的代码可以提供帮助,这样您就可以分别使用相同的路径进行 POST、GET、PUT、DELETE 并编写自己的逻辑。

I have Modified your Code a Little Bit, Here below is the code:我稍微修改了你的代码,下面是代码:

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)

If you have to use the Model Object with product, You can use something like the below, I have Created a full fledged example for your reference.如果您必须将 Model Object 与产品一起使用,您可以使用类似下面的内容,我已经创建了一个完整的示例供您参考。

Folder Structure:文件夹结构:

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

product.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)

data.py数据.py

from models import Product
import datetime

test_product = Product()

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

main.py主文件

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)

models.py模型.py

from sanic_openapi import doc

class Product:
    id = int
    name = str

Now running python main.py现在运行python main.py

Console Output:控制台 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]

Accessing Swagger UI with API:使用 API 访问 Swagger UI:

If You have to use a Class based view Demonstration, Here below are the two files which can help.如果您必须使用基于 Class 的视图演示,以下是可以提供帮助的两个文件。

Project Structure
 Project
     main.py
     blueprint.py

main.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)

blueprint.py蓝图.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)

Now run python main.py现在运行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]

You can now see the SWAGGER UI at http://localhost:8000/swagger/您现在可以在http://localhost:8000/swagger/看到 SWAGGER UI

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

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