Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions jsonpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ def apply(self, obj):

try:
del subobj[part]
except (KeyError, IndexError) as ex:
except (KeyError, IndexError, TypeError) as ex:
# TypeError happens when subobj is not a mutable container, e.g. a
# pointer into a string value ("str object doesn't support item
# deletion").
msg = "can't remove a non-existent object '{0}'".format(part)
raise JsonPatchConflict(msg)
Comment on lines +249 to 254

Expand Down Expand Up @@ -379,7 +382,9 @@ def apply(self, obj):
subobj, part = from_ptr.to_last(obj)
try:
value = subobj[part]
except (KeyError, IndexError) as ex:
except (KeyError, IndexError, TypeError) as ex:
# TypeError happens when the 'from' pointer ends in '-' (the
# array-append token), so part is a string used to index a list.
raise JsonPatchConflict(str(ex))

# If source and target are equal, this is a no-op
Expand Down Expand Up @@ -489,7 +494,9 @@ def apply(self, obj):
subobj, part = from_ptr.to_last(obj)
try:
value = copy.deepcopy(subobj[part])
except (KeyError, IndexError) as ex:
except (KeyError, IndexError, TypeError) as ex:
# TypeError happens when the 'from' pointer ends in '-' (the
# array-append token), so part is a string used to index a list.
raise JsonPatchConflict(str(ex))

obj = AddOperation({
Expand Down
19 changes: 19 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,25 @@ def test_remove_keyerror_dict(self):
patch_obj = [ { "op": "remove", "path": "/foo/non-existent"} ]
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)

def test_copy_from_dash_on_array(self):
# 'from' ending in '-' indexes a list with a string; used to raise a
# bare TypeError.
src = [1, 2, 3]
patch_obj = [ { "op": "copy", "path": "/0", "from": "/-"} ]
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)

def test_move_from_dash_on_array(self):
src = [1, 2, 3]
patch_obj = [ { "op": "move", "path": "/0", "from": "/-"} ]
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)

def test_remove_index_into_string(self):
# A pointer into a string value is not a mutable container; deleting
# from it used to raise a bare TypeError.
src = {"foo": "bar"}
patch_obj = [ { "op": "remove", "path": "/foo/0"} ]
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)

def test_insert_oob(self):
src = {"foo": [1, 2]}
patch_obj = [ { "op": "add", "path": "/foo/10", "value": 1} ]
Expand Down