FastAPI Helpers¶
This module contains the FastAPI integration layer: error handlers, content-type enforcement helpers, OpenAPI request-body helpers, and route configuration utilities.
FastAPI integration helpers for jsonpatch.
Default error mapping: - 415: Wrong Content-Type for JSON Patch (application/json-patch+json) - 422: Request validation errors (malformed JSON, invalid operationns or pointers, model revalidation failure) - 409: Patch is valid but cannot be applied to current resource state - 500: Server misconfiguration or unexpected failures (e.g., invalid registry/op classes)
JsonPatchRoute
dataclass
¶
Configure JSON Patch routes with a single source of truth.
Source code in jsonpatchx/fastapi.py
Body()
¶
Return the configured FastAPI Body(...) parameter.
Source code in jsonpatchx/fastapi.py
route_kwargs()
¶
Return FastAPI route kwargs for this JSON Patch contract.
Source code in jsonpatchx/fastapi.py
install_jsonpatch_error_handlers(app)
¶
Register a FastAPI exception handler for PatchError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application to configure. |
required |
Examples:
app = FastAPI()
install_jsonpatch_error_handlers(app)
Source code in jsonpatchx/fastapi.py
patch_content_type_dependency(enabled, *, media_type=JSON_PATCH_MEDIA_TYPE)
¶
Return a dependency list that enforces the JSON Patch media type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
Whether content-type enforcement should be enabled. |
required |
media_type
|
str
|
The accepted JSON Patch media type. |
JSON_PATCH_MEDIA_TYPE
|
Returns:
| Type | Description |
|---|---|
list[Depends]
|
A dependency list suitable for FastAPI route decorators. |
Examples:
@app.patch("/items/{item_id}", dependencies=patch_content_type_dependency(True))
def patch_item(...):
...
Source code in jsonpatchx/fastapi.py
patch_error_openapi_responses()
¶
Return OpenAPI response schema entries for JSON Patch errors.
Returns:
| Type | Description |
|---|---|
dict[int | str, dict[str, Any]]
|
A |
dict[int | str, dict[str, Any]]
|
|
Examples:
@app.patch("/items/{item_id}", responses=patch_error_openapi_responses())
def patch_item(...):
...
Source code in jsonpatchx/fastapi.py
patch_request_body(patch_model, examples=None, *, allow_application_json=False, media_type=JSON_PATCH_MEDIA_TYPE, request_body_overrides=None)
¶
Build an OpenAPI requestBody for JSON Patch with optional examples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patch_model
|
type[JsonPatchFor[Any, Any]]
|
The generated |
required |
examples
|
dict[str, Any] | None
|
Optional OpenAPI examples keyed by example name. |
None
|
allow_application_json
|
bool
|
Whether |
False
|
media_type
|
str
|
The primary JSON Patch media type to document. |
JSON_PATCH_MEDIA_TYPE
|
request_body_overrides
|
dict[str, Any] | None
|
Optional shallow overrides for the generated
top-level |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
An |
Notes
JSON Patch requests should use application/json-patch+json. This
library advertises only that media type by default. Set
allow_application_json=True to also document application/json when
you intentionally accept both.
Examples:
@app.patch(
"/configs/{config_id}",
openapi_extra=patch_request_body(ConfigPatch, examples={"set": {...}}),
)
def patch_config(...):
...
# Add an extra media type and override requestBody.required
openapi_extra=patch_request_body(
ConfigPatch,
request_body_overrides={
"required": False,
"content": {"application/merge-patch+json": {"schema": {"type": "object"}}},
},
)
Source code in jsonpatchx/fastapi.py
patch_route_kwargs(patch_model=None, examples=None, *, allow_application_json=False, media_type=JSON_PATCH_MEDIA_TYPE, request_body_overrides=None)
¶
Return FastAPI decorator kwargs that keep docs and enforcement aligned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patch_model
|
type[JsonPatchFor[Any, Any]] | None
|
Optional patch model used to generate request-body docs. |
None
|
examples
|
dict[str, Any] | None
|
Optional OpenAPI examples for the patch request body. |
None
|
allow_application_json
|
bool
|
Whether |
False
|
media_type
|
str
|
The primary JSON Patch media type to document. |
JSON_PATCH_MEDIA_TYPE
|
request_body_overrides
|
dict[str, Any] | None
|
Optional shallow overrides for the generated
|
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A FastAPI route kwargs mapping containing |
dict[str, Any]
|
|
Notes
If allow_application_json is true, application/json is documented
and content-type enforcement is disabled to allow both media types.