如何使用另一个 swagger 文档扩展 FastAPI 文档?
How to extend FastAPI docs with another swagger docs?
我决定在Python的FastApi框架中做一个微服务网关。我的授权服务是用 Django 编写的,并且已经由 drf-yasg
package swagger 文档生成。我在想是否有办法以某种方式将 auth 的架构导入网关。我可以通过 http 以 json
格式提供模式并从网关访问它。问题是如何将 FastApi 的文档与原始的 swagger 模式文件集成。
根据docs可以修改openAPI json.
示例:
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
结果:
我决定在Python的FastApi框架中做一个微服务网关。我的授权服务是用 Django 编写的,并且已经由 drf-yasg
package swagger 文档生成。我在想是否有办法以某种方式将 auth 的架构导入网关。我可以通过 http 以 json
格式提供模式并从网关访问它。问题是如何将 FastApi 的文档与原始的 swagger 模式文件集成。
根据docs可以修改openAPI json.
示例:
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
结果: