Skip to content

Commit 7583258

Browse files
authored
Merge pull request #65 from thunderstruck47/master
fixing array diff bug (issue #30)
2 parents f0a4f51 + 845cf4a commit 7583258

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

jsonpatch.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ def make_patch(src, dst):
175175
# TODO: fix patch optimiztion and remove the following check
176176
# fix when patch with optimization is incorrect
177177
patch = JsonPatch.from_diff(src, dst)
178-
new = patch.apply(src)
178+
try:
179+
new = patch.apply(src)
180+
except JsonPatchConflict: # see TODO
181+
return JsonPatch.from_diff(src, dst, False)
182+
179183
if new != dst:
180184
return JsonPatch.from_diff(src, dst, False)
181185

@@ -601,7 +605,6 @@ def _longest_common_subseq(src, dst):
601605
matrix[i][j] = matrix[i-1][j-1] + 1
602606
if matrix[i][j] > z:
603607
z = matrix[i][j]
604-
if matrix[i][j] == z:
605608
range_src = (i-z+1, i+1)
606609
range_dst = (j-z+1, j+1)
607610
else:

tests.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,15 @@ def test_json_patch(self):
376376
patch = jsonpatch.make_patch(old, new)
377377
new_from_patch = jsonpatch.apply_patch(old, patch)
378378
self.assertEqual(new, new_from_patch)
379-
379+
380+
def test_arrays_one_element_sequences(self):
381+
""" Tests the case of multiple common one element sequences inside an array """
382+
# see https://github.com/stefankoegl/python-json-patch/issues/30#issuecomment-155070128
383+
src = [1,2,3]
384+
dst = [3,1,4,2]
385+
patch = jsonpatch.make_patch(src, dst)
386+
res = jsonpatch.apply_patch(src, patch)
387+
self.assertEqual(res, dst)
380388

381389
class OptimizationTests(unittest.TestCase):
382390
def test_use_replace_instead_of_remove_add(self):

0 commit comments

Comments
 (0)