https://github.com/alexdemure/gadfastopenapi
Customize and override OpenAPI schema details in FastAPI with ease
https://github.com/alexdemure/gadfastopenapi
fastapi fastapi-extension openapi openapi-codegen
Last synced: 7 months ago
JSON representation
Customize and override OpenAPI schema details in FastAPI with ease
- Host: GitHub
- URL: https://github.com/alexdemure/gadfastopenapi
- Owner: AlexDemure
- License: mit
- Created: 2025-03-24T14:00:31.000Z (10 months ago)
- Default Branch: production
- Last Pushed: 2025-04-26T08:24:47.000Z (9 months ago)
- Last Synced: 2025-05-29T07:17:35.772Z (8 months ago)
- Topics: fastapi, fastapi-extension, openapi, openapi-codegen
- Language: Python
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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}
```
