python-json-patch icon indicating copy to clipboard operation
python-json-patch copied to clipboard

"can't remove non-existent object" exception with duplicate dicts in list

Open symroe opened this issue 7 years ago • 1 comments

Here's a test case that fails in 1.23:

from jsonpatch import JsonPatch                                                                                                                                           
from_v = { 
    "foo": [ 
        {"baz": 2}, 
        {"bar": 1}, 
        {"bar": 1}, 
    ] 
}                                                                                                                                                                 

to_v = { 
    "foo": [ 
        {"baz": 2}, 
    ] 
}                                                                                                                                                                 

x = JsonPatch.from_diff(from_v, to_v)                                                                                                                                         
x.apply(to_v) 

The exception is:

Traceback (most recent call last):
  File "/Users/symroe/.envs/jsonpatch_test/lib/python3.6/site-packages/jsonpatch.py", line 385, in apply
    del subobj[part]
IndexError: list assignment index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "jsonpatch_bug.py", line 18, in <module>
    x.apply(to_v)  
  File "/Users/symroe/.envs/jsonpatch_test/lib/python3.6/site-packages/jsonpatch.py", line 312, in apply
    obj = operation.apply(obj)
  File "/Users/symroe/.envs/jsonpatch_test/lib/python3.6/site-packages/jsonpatch.py", line 388, in apply
    raise JsonPatchConflict(msg)
jsonpatch.JsonPatchConflict: can't remove non-existent object '1'

It looks like having two identical dicts in a list causes this, when both dicts are removed from to_v.

symroe avatar Jan 23 '19 14:01 symroe

I am having a similar issue, using the example in the docs

Python 3.6.6
import jsonpatch
src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
patch = jsonpatch.make_patch(src, dst)
result = patch.apply(doc)

Traceback (most recent call last):
  File "/home/rbelgrave/.local/share/virtualenvs/test-RQFjY04W/lib/python3.6/site-packages/jsonpatch.py", line 385, in apply
    del subobj[part]
KeyError: 'foo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rbelgrave/.local/share/virtualenvs/test-RQFjY04W/lib/python3.6/site-packages/jsonpatch.py", line 312, in apply
    obj = operation.apply(obj)
  File "/home/rbelgrave/.local/share/virtualenvs/test-RQFjY04W/lib/python3.6/site-packages/jsonpatch.py", line 388, in apply
    raise JsonPatchConflict(msg)
jsonpatch.JsonPatchConflict: can't remove non-existent object 'foo'

rmb938 avatar Feb 03 '19 19:02 rmb938