Skip to content

Merge patch

rn_forge.web.merge_patch

RFC 7396 JSON Merge Patch.

Hand-rolled: no clearly maintained package implements RFC 7396, and the algorithm is a few lines.

MERGE_PATCH_MEDIA_TYPE = 'application/merge-patch+json' module-attribute

The media type of a JSON Merge Patch body.

apply_merge_patch(target, patch)

Apply patch to target as RFC 7396 section 2 specifies.

A non-object patch replaces target. An object patch merges into target (an empty object when target is not one): a null member removes the member, any other member is merged recursively, and arrays are replaced whole.

Parameters:

Name Type Description Default
target JsonValue

The document to patch. Not modified.

required
patch JsonValue

The merge patch. Not modified.

required

Returns:

Type Description
JsonValue

The patched document, sharing no mutable state with either argument.

merge_representation(current, patch)

Merge patch into the resource representation current.

At the top level a null member sets that field to None and keeps it. Every other member, and everything below the top level, follows :func:apply_merge_patch.

Parameters:

Name Type Description Default
current Mapping[str, JsonValue]

The resource's current representation. Not modified.

required
patch Mapping[str, JsonValue]

The merge patch object. Not modified.

required

Returns:

Type Description
dict[str, JsonValue]

The merged representation.

require_patch_object(body)

Return body when it is a JSON object.

Raises:

Type Description
InvalidMergePatch

body is not a JSON object.