diff options
author | Stefan Kögl <stefan@skoegl.net> | 2020-01-29 09:15:49 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-29 09:15:49 +0100 |
commit | 91f61241adc9d104e1811eeaf4d9dc6518d6786e (patch) | |
tree | 400d360df7c4ef17095931ddfbc0b7f303c9b1ed | |
parent | b3726f3a8bdcdf0f0841e078228014de8477b0ec (diff) | |
parent | c1fce712bea25d2fb33b843ccc8f4cd0fca7361d (diff) | |
download | python-json-patch-91f61241adc9d104e1811eeaf4d9dc6518d6786e.tar.gz |
Merge pull request #106 from blakehilliard/only-type-diff
Fix issue not recognizing that, in JSON, 1 != 1.0 != True
-rw-r--r-- | jsonpatch.py | 15 | ||||
-rwxr-xr-x | tests.py | 18 |
2 files changed, 29 insertions, 4 deletions
diff --git a/jsonpatch.py b/jsonpatch.py index a474eae..7f31ce5 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -817,10 +817,7 @@ class DiffBuilder(object): 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) @@ -828,6 +825,16 @@ class DiffBuilder(object): 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) @@ -427,6 +427,24 @@ class MakePatchTestCase(unittest.TestCase): 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): |