Skip to content

Custom app.openapi

override_validation_error wraps the app.openapi function the app has when you call it, and patches the schema that function returns. Assign your own function first:

def custom_openapi() -> dict[str, Any]:
    if app.openapi_schema:
        return app.openapi_schema
    schema = get_openapi(
        title="Inventory API", version="2.1.0", routes=app.routes
    )
    schema["info"]["x-logo"] = {"url": "https://example.com/logo.png"}
    app.openapi_schema = schema
    return schema


app.openapi = custom_openapi  # type: ignore[method-assign]
override_validation_error(app)

The schema keeps your title, version and x-logo, and documents the validation error at 400. The caching in your function keeps working: the patch is applied to the schema it returns.

Assigned after the call, your function replaces the wrapper and the schema is no longer patched, while the response still uses the new status code.