Skip to main content

Customize and override OpenAPI schema details in FastAPI with ease

Project description

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}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gadfastopenapi-0.0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gadfastopenapi-0.0.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file gadfastopenapi-0.0.1.tar.gz.

File metadata

  • Download URL: gadfastopenapi-0.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for gadfastopenapi-0.0.1.tar.gz
Algorithm Hash digest
SHA256 061b554cbb161a71cc30be9992e6c2ce1dd9d536a478e52090e09cfbaa2f45e3
MD5 c9fe974ad7927418098015a5e1cd1983
BLAKE2b-256 2e3ea0a8b04ba287afddab84bf5411f2305110f2b3a1dbd773a78b53ab461512

See more details on using hashes here.

File details

Details for the file gadfastopenapi-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: gadfastopenapi-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for gadfastopenapi-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 66e2091de8cd9a3be26c67ae71ccbad40a65df1f3b14cbb4d9b8a498f040f2bf
MD5 aa79ea93b7f5870198d14859d088b84d
BLAKE2b-256 26e1aa4adbf04f85e18278d2e53d34cbb8c3651d7d0dd83aa770d2e4ecd31d78

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page