Skip to content

Commit c9ac1d9

Browse files
committed
Merge pull request #50 from go1dshtein/master
Fix KeyError in add/remove optimization
2 parents 5cc9bee + 72a90e3 commit c9ac1d9

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

jsonpatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ def _optimize_using_replace(prev, cur):
767767
if cur['op'] == 'add':
768768
# make recursive patch
769769
patch = make_patch(prev['value'], cur['value'])
770-
if len(patch.patch) == 1:
770+
if len(patch.patch) == 1 and patch.patch[0]['op'] != 'remove':
771771
prev['path'] = prev['path'] + patch.patch[0]['path']
772772
prev['value'] = patch.patch[0]['value']
773773
else:

tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ def test_use_replace_instead_of_remove_add(self):
317317
res = jsonpatch.apply_patch(src, patch)
318318
self.assertEqual(res, dst)
319319

320+
def test_use_replace_instead_of_remove_add_nested(self):
321+
src = {'foo': [{'bar': 1, 'baz': 2}, {'bar': 2, 'baz': 3}]}
322+
dst = {'foo': [{'bar': 1}, {'bar': 2, 'baz': 3}]}
323+
patch = list(jsonpatch.make_patch(src, dst))
324+
self.assertEqual(len(patch), 1)
325+
self.assertEqual(patch[0]['op'], 'replace')
326+
res = jsonpatch.apply_patch(src, patch)
327+
self.assertEqual(res, dst)
328+
320329
def test_use_move_instead_of_remove_add(self):
321330
src = {'foo': [4, 1, 2, 3]}
322331
dst = {'foo': [1, 2, 3, 4]}

0 commit comments

Comments
 (0)