報錯“Please indicate a valid Swagger or OpenAPI version field”

九尾cat發表於2024-05-01

報錯“Please indicate a valid Swagger or OpenAPI 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).

原因分析

根據報錯資訊,分析是沒指定版本資訊

程式碼修改

def custom_openapi():
    if application.openapi_schema:
        return application.openapi_schema
    openapi_schema = get_openapi(
        description=settings.DESCRIPTION,
        version=settings.VERSION,
        title=settings.PROJECT_NAME,
        routes=app.routes,
    )
    openapi_schema["openapi"] = "3.0.0"
    openapi_schema["info"]["version"] = settings.VERSION
    openapi_schema["info"]["x-logo"] = {
        "url": "/static/logo-teal.png"
    }
    application.openapi_schema = openapi_schema
    return application.openapi_schema

相關文章