How to extend FastAPI docs with another swagger docs?

纵然是瞬间 提交于 2021-01-27 16:15:09

问题


I decided to make a micro-services gateway in Python's FastApi framework. My authorization service is written in Django and there are already generated by drf-yasg package swagger docs. I was thinking if there is a way to somehow import auth's schema to the gateway. I can serve the schema in json format via http and access it from the gateway. The question is how to integrate FastApi's docs with raw swagger schema file.


回答1:


According to docs you can modify the openAPI json.

Example:

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi

app = FastAPI()


@app.get("/items/")
async def read_items():
    return [{"name": "Foo"}]


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Custom title",
        version="2.5.0",
        description="This is a very custom OpenAPI schema",
        routes=app.routes,
    )
    openapi_schema["paths"]["/api/auth"] = {
        "post": {
            "requestBody": {"content": {"application/json": {}}, "required": True}, "tags": ["Auth"]
        }
    }
    app.openapi_schema = openapi_schema
    return app.openapi_schema


app.openapi = custom_openapi

Result:



来源:https://stackoverflow.com/questions/64401878/how-to-extend-fastapi-docs-with-another-swagger-docs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!