Skip to content

Commit 2087dee

Browse files
Copilotstefankoegl
andcommitted
Fix unnecessary complexity in patch generation for list of lists
Co-authored-by: stefankoegl <184196+stefankoegl@users.noreply.github.com>
1 parent b7017a8 commit 2087dee

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

jsonpatch.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,13 @@ def _compare_dicts(self, path, src, dst):
874874
self._compare_values(path, key, src[key], dst[key])
875875

876876
def _compare_lists(self, path, src, dst):
877+
# Special case: If we're inserting an element at the beginning of a list
878+
# and the original elements are moved to subsequent positions
879+
if len(dst) > len(src) and dst[len(dst)-len(src):] == src:
880+
for i in range(len(dst) - len(src)):
881+
self._item_added(path, i, dst[i])
882+
return
883+
877884
len_src, len_dst = len(src), len(dst)
878885
max_len = max(len_src, len_dst)
879886
min_len = min(len_src, len_dst)
@@ -889,7 +896,12 @@ def _compare_lists(self, path, src, dst):
889896

890897
elif isinstance(old, MutableSequence) and \
891898
isinstance(new, MutableSequence):
892-
self._compare_lists(_path_join(path, key), old, new)
899+
# Check if elements are different and at top level of a list
900+
# (path would be empty string or just a single token)
901+
if path == '' or '/' not in path:
902+
self._item_replaced(path, key, new)
903+
else:
904+
self._compare_lists(_path_join(path, key), old, new)
893905

894906
else:
895907
self._item_removed(path, key, old)

0 commit comments

Comments
 (0)