如何用另一个笨拙的文档扩展FastAPI文档? [英] How to extend FastAPI docs with another swagger docs?

查看:76
本文介绍了如何用另一个笨拙的文档扩展FastAPI文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定在Python的FastApi框架中建立一个微服务网关.我的授权服务是用Django编写的,并且已经由 drf-yasg 软件包swagger docs生成.我在想是否有办法以某种方式将身份验证的架构导入网关.我可以通过http以 json 格式提供模式,并从网关访问它.问题是如何将FastApi的文档与原始的swagger模式文件集成.

解决方案

根据

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.

解决方案

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:

这篇关于如何用另一个笨拙的文档扩展FastAPI文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆