'Separate FastApi documentation into sections
Currently the OpenAPI documentation looks like this: Is it possible to separate it into multiple sections?
For example, 2 sections, one being the "books" section that contains the methods from "/api/bookcollection/books/" endpoints and the other containing the endpoints with "/api/bookcollection/authors/".
I have consulted the FastApi documentation, but I do not find anything close to the operation I want to do.
Solution 1:[1]
The OpenAPI
allows the use of tags to group endpoints. FastAPI
also supports this feature. The documentation section can be found here.
Example:
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"}]
Solution 2:[2]
Another solution for this would be just to create different routers for every resource that you want to have in different documentation sections.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | alex_noname |
Solution 2 | Ionut-Alexandru Baltariu |