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

Bug: Remove /array/- raises TypeError

Open Ventilateur opened this issue 4 years ago • 5 comments

Version: 1.32 Python version: 3.8

Description Removing array element with dash notation raises an unexpected TypeError. Like so: {'op': 'remove', 'path': '/vals/-'}.
I understand that the dash points to the end of array, but not the last element? Even so, if the syntax is invalid, I think it should raise a JsonPatchException or JsonPointerException.

Step to reproduce

jsonpatch.JsonPatch([
        {'op': 'remove', 'path': '/vals/-'}
    ]).apply({
        'vals': [1, 2, 3]
    })

Stack trace

File "\lib\site-packages\jsonpatch.py", line 669, in apply
    obj = operation.apply(obj)
  File "\lib\site-packages\jsonpatch.py", line 238, in apply
    del subobj[part]
TypeError: list indices must be integers or slices, not str

Ventilateur avatar Apr 02 '21 13:04 Ventilateur

Thanks for the report. I think your suggestion makes a lot of sense.

Would you be interested in preparing a pull request?

stefankoegl avatar Apr 03 '21 08:04 stefankoegl

Ok, I will find some time to do it. So according to this section, I think it make sense to raise a JsonPointerException in this case. What do you think?

Ventilateur avatar Apr 03 '21 10:04 Ventilateur

Agreed - makes sense

stefankoegl avatar Apr 03 '21 18:04 stefankoegl

FYI I made a PR, can you check please?

Ventilateur avatar Apr 08 '21 13:04 Ventilateur

Looks good! I’ll merge and include it in the next release. Thanks!

stefankoegl avatar Apr 08 '21 19:04 stefankoegl