将 FastApi 文档分成几个部分

Separate FastApi documentation into sections

目前 OpenAPI 文档如下所示: 是否可以将其分成多个部分?

例如,2 个部分,一个是包含来自“/api/bookcollection/books/”端点的方法的“书籍”部分,另一个包含带有“/api/bookcollection/authors/”的端点。

我查阅了 FastApi documentation,但我没有找到任何接近我想做的操作。

OpenAPI allows the use of tags to group endpoints. FastAPI also supports this feature. The documentation section can be found here.

示例:

from fastapi import FastAPI

tags_metadata = [
    {
        "name": "users",
        "description": "Operations with users. The **login** logic is also here.",
    },
    {
        "name": "items",
        "description": "Manage items. So _fancy_ they have their own docs.",
        "externalDocs": {
            "description": "Items external docs",
            "url": "https://fastapi.tiangolo.com/",
        },
    },
]

app = FastAPI(openapi_tags=tags_metadata)


@app.get("/users/", tags=["users"])
async def get_users():
    return [{"name": "Harry"}, {"name": "Ron"}]


@app.get("/items/", tags=["items"])
async def get_items():
    return [{"name": "wand"}, {"name": "flying broom"}]

另一种解决方案是为您希望在不同文档部分中拥有的每种资源创建不同的路由器。

source