Skip to content

Commit d1f317a

Browse files
committed
Fix: optimization bugs stefankoegl#55, stefankoegl#54, add more tests
1 parent 27f1f98 commit d1f317a

File tree

3 files changed

+495
-24
lines changed

3 files changed

+495
-24
lines changed

jsonpatch.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,16 @@ def _optimize_using_replace(prev, cur):
770770
if cur['op'] == 'add':
771771
# make recursive patch
772772
patch = make_patch(prev['value'], cur['value'])
773-
if len(patch.patch) == 1 and patch.patch[0]['op'] != 'remove':
773+
# check case when dict "remove" is less than "add" and has a same key
774+
if isinstance(prev['value'], dict) and isinstance(cur['value'], dict) and len(prev['value'].keys()) == 1:
775+
prev_set = set(prev['value'].keys())
776+
cur_set = set(cur['value'].keys())
777+
if prev_set & cur_set == prev_set:
778+
patch = make_patch(cur['value'], prev['value'])
779+
780+
if len(patch.patch) == 1 and \
781+
patch.patch[0]['op'] != 'remove' and \
782+
patch.patch[0]['path'] and patch.patch[0]['path'].split('/')[1] in prev['value']:
774783
prev['path'] = prev['path'] + patch.patch[0]['path']
775784
prev['value'] = patch.patch[0]['value']
776785
else:

0 commit comments

Comments
 (0)