summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2016-05-31 18:47:33 +0200
committerStefan Kögl <stefan@skoegl.net>2016-05-31 18:47:33 +0200
commitc9ac1d9f0a9ab522614c36beb70b74db45492fc1 (patch)
tree1c2ec577483b9775f11028f11e1fc79d3178632c
parent5cc9bee572f6207166122ac2ba9cecde0598930a (diff)
parent72a90e3ff1e75c6628106509568de9e22ca2b259 (diff)
downloadpython-json-patch-c9ac1d9f0a9ab522614c36beb70b74db45492fc1.tar.gz
Merge pull request #50 from go1dshtein/master
Fix KeyError in add/remove optimization
-rw-r--r--jsonpatch.py2
-rwxr-xr-xtests.py9
2 files changed, 10 insertions, 1 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index 96fe6f9..32508e2 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -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:
diff --git a/tests.py b/tests.py
index 8b0b52c..5acf24b 100755
--- a/tests.py
+++ b/tests.py
@@ -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]}