Skip to main content

logo

Utility toolkit to modify and override OpenAPI schema definitions in FastAPI.


Installation

pip install gadfastopenapi

Usage

from gadfastopenapi import OpenAPI

app.openapi = OpenAPI(app)

# or

@app.get("/api/openapi.json", include_in_schema=False)
async def openapi():
    return OpenAPI(app, handlers=[affix, use_route_as_operation_id]).generate()

Extension gadfastopenapi.extensions.affix

from fastapi import FastAPI
from pydantic import BaseModel

from gadfastopenapi import OpenAPI
from gadfastopenapi.extensions.affix import affix

app = FastAPI()

app.openapi = OpenAPI(app, handlers=[affix])


class Deprecated(BaseModel):
    __affix__ = "Deprecated:"


class User(Deprecated):
    id: int


@app.get("/user", response_model=User)
def get_user():
    return {"id": 1}

openapi.json
>>>
{
  "paths": {
    "schema": {
      "$ref": "#/components/schemas/DeprecatedUser"
    }
  },
  "schemas": {
      "DeprecatedUser": {
        "title": "DeprecatedUser"
      }
    }
}

Extension gadfastopenapi.extensions.operationid

from fastapi import FastAPI
from pydantic import BaseModel

from gadfastopenapi import OpenAPI
from gadfastopenapi.extensions.operationid import use_route_as_operation_id

app = FastAPI()

app.openapi = OpenAPI(app, handlers=[use_route_as_operation_id])


class User(BaseModel):
    id: int


@app.get("/user", response_model=User)
def get_user():
    return {"id": 1}

openapi.json
>>> BEFORE
{
  "paths": {
    "/user": {
      "get": {
        "operationId": "get_user_user_get",
      }
    }
  }
}

>>> AFTER
{
  "paths": {
    "/user": {
      "get": {
        "operationId": "get_user",
      }
    }
  }
}

Extension gadfastopenapi.extensions.errors

from fastapi import FastAPI
from gadfastopenapi.extensions.errors import APIError, openapi_errors

app = FastAPI()


class MyError(APIError):
    ...


@app.get("/user", response_model=dict, responses=openapi_errors(MyError))
def get_user():
    return {"id": 1}

openapi.json
>>>
{
  "paths": {
    "/user": {
      "get": {
        "responses": {
          "200": {
            "description": "Successful Response"
          },
          "418": {
            "description": "MyError",
            "content": {
              "application/json": {
                "example": {
                  "status_code": 418,
                  "detail": {
                    "type": "MyError"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Custom handler

from fastapi import FastAPI
from gadfastopenapi import OpenAPI

def my_handler(app: FastAPI, openapi: dict) -> tuple[FastAPI, dict]:
    # Mutate openapi here
    return app, openapi

app = FastAPI()

app.openapi = OpenAPI(app, handlers=[my_handler])

@app.get("/user", response_model=dict)
def get_user():
    return {"id": 1}

Metadata

Release files for gadfastopenapi 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gadfastopenapi 0.0.1
File Size Uploaded
gadfastopenapi-0.0.1.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gadfastopenapi 0.0.1
File Interpreter ABI Platform
gadfastopenapi-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.9 kB

Release files / gadfastopenapi-0.0.1.tar.gz

Download URL gadfastopenapi-0.0.1.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
061b554cbb161a71cc30be9992e6c2ce1dd9d536a478e52090e09cfbaa2f45e3
BLAKE2b-256 checksum
How to use checksums
2e3ea0a8b04ba287afddab84bf5411f2305110f2b3a1dbd773a78b53ab461512
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / gadfastopenapi-0.0.1-py3-none-any.whl

Download URL gadfastopenapi-0.0.1-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66e2091de8cd9a3be26c67ae71ccbad40a65df1f3b14cbb4d9b8a498f040f2bf
BLAKE2b-256 checksum
How to use checksums
26e1aa4adbf04f85e18278d2e53d34cbb8c3651d7d0dd83aa770d2e4ecd31d78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page