如何为FastAPI生成swagger 2.0文档

2024-04-23 10:01:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用FastAPI开发一个微服务,并将其部署到云运行。我正在API网关前面添加API网关。在创建API网关时,它要求我上传API规范文件。因为我使用的是FastAPI,所以生成的文档是针对3.0.2的

我试图重写custom_openapi定义并提供openapi_version,但它不起作用,我得到一个错误

Unable to render this definition The provided definition does not specify a valid version field.

Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).

我的custom_openapi定义是这样的:

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_version="swagger:2.0" # I have tried 2.0

    )

有没有办法为我的FastAPI生成Swagger 2.0文档


Tags: 文档apiappfield网关定义versionschema