Merge patch
rn_forge.fastapi.merge_patch
FastAPI binding for JSON Merge Patch (RFC 7396).
On a PATCH route, declare :func:merge_patch_body before require_if_match, call
:func:rn_forge.web.check_precondition in the route, then merge the body into the stored
resource with :func:merge_into. Add :func:merge_patch_openapi as the route's
openapi_extra.
merge_into(model, current, body)
Merge the merge-patch body into current and return the validated model.
body is parsed as JSON and must be an object. current is dumped through model by alias, merged with
:func:rn_forge.web.merge_representation, and the result validated as a full document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[M]
|
The pydantic model of the resource. |
required |
current
|
object
|
The stored resource: a mapping or an object with matching attributes. |
required |
body
|
bytes
|
The raw request body from :func: |
required |
Raises:
| Type | Description |
|---|---|
InvalidMergePatch
|
body is not valid JSON or not a JSON object. |
RequestValidationError
|
The merged document is invalid. Each error's
location is prefixed with |
merge_patch_body()
Return a dependency yielding the raw request body.
The dependency raises :class:rn_forge.web.UnsupportedMediaType (415, with
Accept-Patch) unless the request's media type, ignoring parameters, is
application/merge-patch+json. The body is returned as bytes, unparsed; pass it to
:func:merge_into.
merge_patch_openapi()
Return the openapi_extra declaring a merge-patch request body.