Skip to content

Wrong Patch Generated #138

Open
Open
@rmorshea

Description

@rmorshea

The following sample of code ought to work, but instead it raises an error:

from jsonpatch import apply_patch, make_patch

old = [
    {"x": ["a", {"y": ["b"]}], "z": "a"},
    {"x": ["c", {"d": ["d"]}], "z": "c"},
    {},
]

new = [
    {"x": ["c", {"y": ["d"]}], "z": "c"},
    {},
]

patch = make_patch(old, new)

assert apply_patch(old, patch) == new
Traceback (most recent call last):
  File "/venv/lib/python3.9/site-packages/jsonpointer.py", line 275, in walk
    return doc[part]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/temp.py", line 18, in <module>
    assert apply_patch(old, patch) == new
  File "/venv/lib/python3.9/site-packages/jsonpatch.py", line 151, in apply_patch
    return patch.apply(doc, in_place)
  File "/venv/lib/python3.9/site-packages/jsonpatch.py", line 669, in apply
    obj = operation.apply(obj)
  File "/venv/lib/python3.9/site-packages/jsonpatch.py", line 324, in apply
    subobj, part = self.pointer.to_last(obj)
  File "/venv/lib/python3.9/site-packages/jsonpointer.py", line 194, in to_last
    doc = self.walk(doc, part)
  File "/venv/lib/python3.9/site-packages/jsonpointer.py", line 278, in walk
    raise JsonPointerException("index '%s' is out of bounds" % (part, ))
jsonpointer.JsonPointerException: index '1' is out of bounds

The generated patch is:

[
    {"op": "remove", "path": "/0/x/0"},
    {"op": "replace", "path": "/0/x/1/y/0", "value": "d"},
    {"op": "replace", "path": "/0/z", "value": "c"},
    {"op": "remove", "path": "/1/x"},
    {"op": "move", "from": "/1/z", "path": "/0/x/0"},
    {"op": "remove", "path": "/2"},
]

Which is clearly the result of the error above.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions