【问题标题】:How to extend FastAPI docs with another swagger docs?如何使用另一个 swagger 文档扩展 FastAPI 文档?
【发布时间】:2021-01-31 17:26:26
【问题描述】:
我决定在 Python 的 FastApi 框架中制作一个微服务网关。我的授权服务是用 Django 编写的,并且已经由 drf-yasg package swagger docs 生成。我在想是否有办法以某种方式将身份验证的模式导入网关。我可以通过 http 以json 格式提供模式并从网关访问它。问题是如何将 FastApi 的文档与原始的 swagger 架构文件集成。
【问题讨论】:
标签:
python
swagger
openapi
fastapi
drf-yasg
【解决方案1】:
根据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
结果: