Fix KeyError in add/remove optimization

This commit is contained in:
Kirill Goldshtein 2016-05-04 09:53:57 +03:00
parent 5cc9bee572
commit 72a90e3ff1
2 changed files with 10 additions and 1 deletions

View File

@ -767,7 +767,7 @@ def _optimize_using_replace(prev, cur):
if cur['op'] == 'add':
# make recursive patch
patch = make_patch(prev['value'], cur['value'])
if len(patch.patch) == 1:
if len(patch.patch) == 1 and patch.patch[0]['op'] != 'remove':
prev['path'] = prev['path'] + patch.patch[0]['path']
prev['value'] = patch.patch[0]['value']
else:

View File

@ -317,6 +317,15 @@ class MakePatchTestCase(unittest.TestCase):
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)
def test_use_replace_instead_of_remove_add_nested(self):
src = {'foo': [{'bar': 1, 'baz': 2}, {'bar': 2, 'baz': 3}]}
dst = {'foo': [{'bar': 1}, {'bar': 2, 'baz': 3}]}
patch = list(jsonpatch.make_patch(src, dst))
self.assertEqual(len(patch), 1)
self.assertEqual(patch[0]['op'], 'replace')
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)
def test_use_move_instead_of_remove_add(self):
src = {'foo': [4, 1, 2, 3]}
dst = {'foo': [1, 2, 3, 4]}