Skip to content

Commit

Permalink
Merge pull request #106 from blakehilliard/only-type-diff
Browse files Browse the repository at this point in the history
Fix issue not recognizing that, in JSON, 1 != 1.0 != True
  • Loading branch information
stefankoegl committed Jan 29, 2020
2 parents b3726f3 + c1fce71 commit 91f6124
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 11 additions & 4 deletions jsonpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,17 +817,24 @@ def _compare_lists(self, path, src, dst):
self._item_added(path, key, dst[key])

def _compare_values(self, path, key, src, dst):
if src == dst:
return

elif isinstance(src, MutableMapping) and \
if isinstance(src, MutableMapping) and \
isinstance(dst, MutableMapping):
self._compare_dicts(_path_join(path, key), src, dst)

elif isinstance(src, MutableSequence) and \
isinstance(dst, MutableSequence):
self._compare_lists(_path_join(path, key), src, dst)

# To ensure we catch changes to JSON, we can't rely on a simple
# src == dst, or it would not recognize the difference between
# 1 and True, among other things. Using json.dumps is the most
# fool-proof way to ensure we catch type changes that matter to JSON
# and ignore those that don't. The performance of this could be
# improved by doing more direct type checks, but we'd need to be
# careful to accept type changes that don't matter when JSONified.
elif json.dumps(src) == json.dumps(dst):
return

else:
self._item_replaced(path, key, dst)

Expand Down
18 changes: 18 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,24 @@ def test_move_from_numeric_to_alpha_dict_key(self):
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)

def test_issue90(self):
"""In JSON 1 is different from True even though in python 1 == True"""
src = {'A': 1}
dst = {'A': True}
patch = jsonpatch.make_patch(src, dst)
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)
self.assertIsInstance(res['A'], bool)

def test_issue103(self):
"""In JSON 1 is different from 1.0 even though in python 1 == 1.0"""
src = {'A': 1}
dst = {'A': 1.0}
patch = jsonpatch.make_patch(src, dst)
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)
self.assertIsInstance(res['A'], float)



class OptimizationTests(unittest.TestCase):
Expand Down

0 comments on commit 91f6124

Please sign in to comment.