Skip to content

Commit ad66344

Browse files
committed
Catch KeyError when accessing the sub-doc items
1 parent d725141 commit ad66344

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

jsonpatch.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def apply(self, obj):
398398
subobj, part = self.pointer.to_last(obj)
399399
try:
400400
del subobj[part]
401-
except IndexError as ex:
401+
except (KeyError, IndexError) as ex:
402402
raise JsonPatchConflict(str(ex))
403403

404404
return obj
@@ -464,7 +464,10 @@ class MoveOperation(PatchOperation):
464464
def apply(self, obj):
465465
from_ptr = JsonPointer(self.operation['from'])
466466
subobj, part = from_ptr.to_last(obj)
467-
value = subobj[part]
467+
try:
468+
value = subobj[part]
469+
except (KeyError, IndexError) as ex:
470+
raise JsonPatchConflict(str(ex))
468471

469472
if isinstance(subobj, dict) and self.pointer.contains(from_ptr):
470473
raise JsonPatchException('Cannot move values into its own children')
@@ -512,7 +515,10 @@ class CopyOperation(PatchOperation):
512515
def apply(self, obj):
513516
from_ptr = JsonPointer(self.operation['from'])
514517
subobj, part = from_ptr.to_last(obj)
515-
value = copy.deepcopy(subobj[part])
518+
try:
519+
value = copy.deepcopy(subobj[part])
520+
except (KeyError, IndexError) as ex:
521+
raise JsonPatchConflict(str(ex))
516522

517523
obj = AddOperation({
518524
'op': 'add',

tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ def test_replace_array_item(self):
7575
'value': 'boo'}])
7676
self.assertEqual(res['foo'], ['bar', 'boo', 'baz'])
7777

78+
def test_move_object_keyerror(self):
79+
obj = {'foo': {'bar': 'baz'},
80+
'qux': {'corge': 'grault'}}
81+
patch_obj = [ {'op': 'move', 'from': '/foo/non-existent', 'path': '/qux/thud'} ]
82+
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, obj, patch_obj)
83+
7884
def test_move_object_key(self):
7985
obj = {'foo': {'bar': 'baz', 'waldo': 'fred'},
8086
'qux': {'corge': 'grault'}}
@@ -94,6 +100,12 @@ def test_move_array_item_into_other_item(self):
94100
res = jsonpatch.apply_patch(obj, patch)
95101
self.assertEqual(res, [{'bar': [{"foo": []}]}])
96102

103+
def test_copy_object_keyerror(self):
104+
obj = {'foo': {'bar': 'baz'},
105+
'qux': {'corge': 'grault'}}
106+
patch_obj = [{'op': 'copy', 'from': '/foo/non-existent', 'path': '/qux/thud'}]
107+
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, obj, patch_obj)
108+
97109
def test_copy_object_key(self):
98110
obj = {'foo': {'bar': 'baz', 'waldo': 'fred'},
99111
'qux': {'corge': 'grault'}}
@@ -314,6 +326,11 @@ def test_remove_keyerror(self):
314326
patch_obj = [ { "op": "remove", "path": "/foo/b"} ]
315327
self.assertRaises(jsonpointer.JsonPointerException, jsonpatch.apply_patch, src, patch_obj)
316328

329+
def test_remove_keyerror_dict(self):
330+
src = {'foo': {'bar': 'barz'}}
331+
patch_obj = [ { "op": "remove", "path": "/foo/non-existent"} ]
332+
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)
333+
317334
def test_insert_oob(self):
318335
src = {"foo": [1, 2]}
319336
patch_obj = [ { "op": "add", "path": "/foo/10", "value": 1} ]

0 commit comments

Comments
 (0)