Skip to content

Commit f935d95

Browse files
authored
Fix _compare_lists to use dumps() comparison instead of == to distinguish 1 from True
1 parent d0bd351 commit f935d95

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

jsonpatch.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -880,17 +880,20 @@ def _compare_lists(self, path, src, dst):
880880
for key in range(max_len):
881881
if key < min_len:
882882
old, new = src[key], dst[key]
883-
if old == new:
884-
continue
885-
886-
elif isinstance(old, MutableMapping) and \
887-
isinstance(new, MutableMapping):
883+
if isinstance(old, MutableMapping) and \
884+
isinstance(new, MutableMapping):
888885
self._compare_dicts(_path_join(path, key), old, new)
889886

890887
elif isinstance(old, MutableSequence) and \
891888
isinstance(new, MutableSequence):
892889
self._compare_lists(_path_join(path, key), old, new)
893890

891+
# To ensure we catch changes to JSON, we can't rely on a
892+
# simple old == new, because it would not recognize the
893+
# difference between 1 and True, among other things.
894+
elif self.dumps(old) == self.dumps(new):
895+
continue
896+
894897
else:
895898
self._item_removed(path, key, old)
896899
self._item_added(path, key, new)

tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,16 @@ def test_issue103(self):
511511
self.assertEqual(res, dst)
512512
self.assertIsInstance(res['A'], float)
513513

514+
def test_issue180(self):
515+
"""In JSON 1 is different from True in list items even though in python 1 == True"""
516+
src = {'aaa': [1, 1, 1]}
517+
dst = {'aaa': [1, True, True]}
518+
patch = jsonpatch.make_patch(src, dst)
519+
res = jsonpatch.apply_patch(src, patch)
520+
self.assertEqual(res, dst)
521+
self.assertIsInstance(res['aaa'][1], bool)
522+
self.assertIsInstance(res['aaa'][2], bool)
523+
514524
def test_issue119(self):
515525
"""Make sure it avoids casting numeric str dict key to int"""
516526
src = [

0 commit comments

Comments
 (0)