Skip to content

Conversation

jg-rp
Copy link
Owner

@jg-rp jg-rp commented Sep 11, 2025

This PR fixes an issue with the JSON Patch add operation. Previously we would raise a JSONPatchError when pointing to an array value with an index equal to the array's length. Now we append to array values when pointing to one past the last index of the array.

To put it another way, when the target value is an array of length 2, /foo/2 is the same as /foo/-

patch = JSONPatch().add(path="/foo/2", value=99)
data = {"foo": ["bar", "baz"]}
assert patch.apply(data) == {"foo": ["bar", "baz", 99]}

# Array length + 1 raises
patch = JSONPatch().add(path="/foo/3", value=99)
data = {"foo": ["bar", "baz"]}
patch.apply(data) # JSONPatchError 

See #117

@jg-rp jg-rp merged commit 9dd1480 into main Sep 12, 2025
26 checks passed
@jg-rp jg-rp deleted the fix-117 branch September 12, 2025 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant