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
install_jsonpatch_error_handlers(app)
¶
Register a FastAPI exception handler for PatchError.
Example:
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.
Example:
@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.
Example:
@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.
Convention: JSON Patch requests should use application/json-patch+json.
This opinionated library advertises only that media type by default.
Set allow_application_json=True to also document application/json for
compatibility when you choose to accept it.
request_body_overrides lets you override top-level requestBody fields and
shallow-merge the content map. If you pass content, its entries are
merged into the generated content; all other keys replace generated values.
Example:
@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.
This opinionated helper adds:
- responses for JSON Patch errors
- dependencies that enforce JSON Patch media type by default
- openapi_extra for the request body when patch_model is provided
If allow_application_json is True, application/json is documented
and enforcement is disabled to allow both.