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

How to group FastAPI endpoints in Swagger UI?

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

提前致谢...

例如,您可以将 tags 添加到您的路径参数中。

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

@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

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

你可以使用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

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