如何在 Swagger UI 中对 FastAPI 端点进行分组?

Ary*_* VR 2 python swagger-ui swagger-2.0 fastapi

我开始使用 fastApi 框架进行编程,它带有一个内置的 swagger 接口来处理请求和响应。我已经完成了近20个api,在swagger界面上很难管理和识别api。有人告诉我在swagger界面中添加部分以区分api,但我在google上找不到任何示例,我需要你们的帮助。

提前致谢...

Yag*_*nci 6

例如,您可以向路径参数添加标签

如果你有这样的事情,使用标签是非常有帮助的。

@app.delete("/items", tags=["Delete Methods"])
@app.put("/items", tags=["Put Methods"])
@app.post("/items", tags=["Post Methods"])
@app.get("/items", tags=["Get Methods"])
async def handle_items():
    return


@app.get("/something", tags=["Get Methods"])
async def something():
    return
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

你会得到这个,如果你想添加描述并且你不想继续重复自己(例如在所有参数中添加相同的描述)

你可以使用openapi_tags(我更喜欢这个)

from fastapi import FastAPI

tags_metadata = [
    {"name": "Get Methods", "description": "One other way around"},
    {"name": "Post Methods", "description": "Keep doing this"},
    {"name": "Delete Methods", "description": "KILL 'EM ALL"},
    {"name": "Put Methods", "description": "Boring"},
]

app = FastAPI(openapi_tags=tags_metadata)


@app.delete("/items", tags=["Delete Methods"])
@app.put("/items", tags=["Put Methods"])
@app.post("/items", tags=["Post Methods"])
@app.get("/items", tags=["Get Methods"])
async def handle_items():
    return
Run Code Online (Sandbox Code Playgroud)

这将提供相同的外观而不会重复

在此处输入图片说明