jsonpatch 1.42__tar.gz → 1.43__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {jsonpatch-1.42/jsonpatch.egg-info → jsonpatch-1.43}/PKG-INFO +1 -1
- {jsonpatch-1.42 → jsonpatch-1.43/jsonpatch.egg-info}/PKG-INFO +1 -1
- {jsonpatch-1.42 → jsonpatch-1.43}/jsonpatch.py +5 -2
- {jsonpatch-1.42 → jsonpatch-1.43}/jsonpatch_cli.py +7 -2
- {jsonpatch-1.42 → jsonpatch-1.43}/tests.py +94 -2
- {jsonpatch-1.42 → jsonpatch-1.43}/AUTHORS +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/LICENSE +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/MANIFEST.in +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/README.md +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/doc/commandline.rst +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/doc/index.rst +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/doc/mod-jsonpatch.rst +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/doc/tutorial.rst +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/ext_tests.py +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/jsonpatch-stubs/__init__.pyi +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/jsonpatch.egg-info/SOURCES.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/jsonpatch.egg-info/dependency_links.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/jsonpatch.egg-info/entry_points.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/jsonpatch.egg-info/requires.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/jsonpatch.egg-info/top_level.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/property_tests.py +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/pyproject.toml +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/pytest.ini +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/requirements.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/setup.cfg +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/setup.py +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/tests.js +0 -0
- {jsonpatch-1.42 → jsonpatch-1.43}/typetests/usage.py +0 -0
|
@@ -55,7 +55,7 @@ _EXACT_MATCH_LIMIT = 500 ** 2
|
|
|
55
55
|
|
|
56
56
|
# Will be parsed by setup.py to determine package metadata
|
|
57
57
|
__author__ = 'Stefan Kögl <stefan@skoegl.net>'
|
|
58
|
-
__version__ = '1.
|
|
58
|
+
__version__ = '1.43'
|
|
59
59
|
__website__ = 'https://github.com/stefankoegl/python-json-patch'
|
|
60
60
|
__license__ = 'BSD-3-Clause'
|
|
61
61
|
|
|
@@ -869,6 +869,9 @@ class JsonPatch(object):
|
|
|
869
869
|
one. The resulting patch transforms `src` into `dst`, so it has to be
|
|
870
870
|
applied to `src` (or to a document equal to it).
|
|
871
871
|
|
|
872
|
+
Values in the patch are copied, so changing either input document
|
|
873
|
+
afterwards does not change the generated patch.
|
|
874
|
+
|
|
872
875
|
:param src: Source JSON document, including arrays and scalar values.
|
|
873
876
|
:type src: object
|
|
874
877
|
|
|
@@ -906,7 +909,7 @@ class JsonPatch(object):
|
|
|
906
909
|
optimization=optimization,
|
|
907
910
|
generate_test_ops=generate_test_ops)
|
|
908
911
|
builder._compare_values((), None, src, dst)
|
|
909
|
-
ops = list(builder.execute())
|
|
912
|
+
ops = copy.deepcopy(list(builder.execute()))
|
|
910
913
|
return cls(ops, pointer_cls=pointer_cls)
|
|
911
914
|
|
|
912
915
|
def to_string(self, dumps=None):
|
|
@@ -8,6 +8,7 @@ import json
|
|
|
8
8
|
import jsonpatch
|
|
9
9
|
import tempfile
|
|
10
10
|
import argparse
|
|
11
|
+
from contextlib import ExitStack
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
jsondiff_parser = argparse.ArgumentParser(
|
|
@@ -35,8 +36,12 @@ def jsondiff_main():
|
|
|
35
36
|
def diff_files():
|
|
36
37
|
""" Diffs two JSON files and prints a patch """
|
|
37
38
|
args = jsondiff_parser.parse_args()
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
with ExitStack() as inputs:
|
|
40
|
+
for stream in (args.FILE1, args.FILE2):
|
|
41
|
+
if stream is not sys.stdin:
|
|
42
|
+
inputs.enter_context(stream)
|
|
43
|
+
doc1 = json.load(args.FILE1)
|
|
44
|
+
doc2 = json.load(args.FILE2)
|
|
40
45
|
patch = jsonpatch.make_patch(doc1, doc2, generate_test_ops=args.test_ops)
|
|
41
46
|
if patch.patch:
|
|
42
47
|
print(json.dumps(patch.patch, indent=args.indent, ensure_ascii=not(args.preserve_unicode)))
|
|
@@ -609,6 +609,36 @@ class CustomTypesJsonPatch(jsonpatch.JsonPatch):
|
|
|
609
609
|
|
|
610
610
|
class MakePatchTestCase(unittest.TestCase):
|
|
611
611
|
|
|
612
|
+
def test_generated_values_do_not_alias_target(self):
|
|
613
|
+
documents = [
|
|
614
|
+
({}, {'a': {'nested': [1]}}),
|
|
615
|
+
({'a': 0}, {'a': {'nested': [1]}}),
|
|
616
|
+
([], [{'nested': [1]}]),
|
|
617
|
+
([0], [{'nested': [1]}]),
|
|
618
|
+
(None, {'a': {'nested': [1]}}),
|
|
619
|
+
]
|
|
620
|
+
for src, dst in documents:
|
|
621
|
+
for optimization in (True, False):
|
|
622
|
+
with self.subTest(src=src, optimization=optimization):
|
|
623
|
+
target = copy.deepcopy(dst)
|
|
624
|
+
patch = jsonpatch.JsonPatch.from_diff(
|
|
625
|
+
src, target, optimization=optimization)
|
|
626
|
+
serialized = patch.to_string()
|
|
627
|
+
value = target[0] if isinstance(target, list) else target['a']
|
|
628
|
+
value['nested'].append(2)
|
|
629
|
+
|
|
630
|
+
self.assertEqual(patch.to_string(), serialized)
|
|
631
|
+
self.assertEqual(patch.apply(src), dst)
|
|
632
|
+
|
|
633
|
+
def test_editing_generated_values_does_not_change_target(self):
|
|
634
|
+
for src in ({}, {'a': 0}):
|
|
635
|
+
with self.subTest(src=src):
|
|
636
|
+
dst = {'a': {'nested': [1]}}
|
|
637
|
+
patch = jsonpatch.make_patch(src, dst)
|
|
638
|
+
patch.patch[0]['value']['nested'].append(2)
|
|
639
|
+
self.assertEqual(dst, {'a': {'nested': [1]}})
|
|
640
|
+
self.assertEqual(patch.apply(src), {'a': {'nested': [1, 2]}})
|
|
641
|
+
|
|
612
642
|
def test_apply_patch_to_copy(self):
|
|
613
643
|
src = {'foo': 'bar', 'boo': 'qux'}
|
|
614
644
|
dst = {'baz': 'qux', 'foo': 'boo'}
|
|
@@ -874,8 +904,13 @@ class MakePatchTestCase(unittest.TestCase):
|
|
|
874
904
|
removed and moved"""
|
|
875
905
|
value = []
|
|
876
906
|
value.append(value)
|
|
877
|
-
|
|
878
|
-
|
|
907
|
+
patch = jsonpatch.make_patch({}, {'a': value}).patch
|
|
908
|
+
self.assertEqual(len(patch), 1)
|
|
909
|
+
self.assertEqual(patch[0]['op'], 'add')
|
|
910
|
+
self.assertEqual(patch[0]['path'], '/a')
|
|
911
|
+
copied = patch[0]['value']
|
|
912
|
+
self.assertIsNot(copied, value)
|
|
913
|
+
self.assertIs(copied[0], copied)
|
|
879
914
|
self.assertEqual(jsonpatch.make_patch({'a': [value]}, {'a': []}).patch,
|
|
880
915
|
[{'op': 'remove', 'path': '/a/0'}])
|
|
881
916
|
self.assertEqual(jsonpatch.make_patch({'a': value}, {'b': value}).patch,
|
|
@@ -2711,6 +2746,63 @@ class CommandLineTests(ScriptRunner, unittest.TestCase):
|
|
|
2711
2746
|
res = self.run_script('jsondiff', src, dst)
|
|
2712
2747
|
self.assertEqual((res.returncode, res.stdout), (0, ''), res.stderr)
|
|
2713
2748
|
|
|
2749
|
+
def test_jsondiff_closes_inputs(self):
|
|
2750
|
+
for target in (1, 2):
|
|
2751
|
+
with self.subTest(target=target):
|
|
2752
|
+
src = self.write('a.json', {'a': 1})
|
|
2753
|
+
dst = self.write('b.json', {'a': target})
|
|
2754
|
+
parser = jsonpatch_cli.jsondiff_parser
|
|
2755
|
+
args = parser.parse_args([src, dst])
|
|
2756
|
+
self.addCleanup(args.FILE1.close)
|
|
2757
|
+
self.addCleanup(args.FILE2.close)
|
|
2758
|
+
output = io.StringIO()
|
|
2759
|
+
with mock.patch.object(parser, 'parse_args', return_value=args), \
|
|
2760
|
+
contextlib.redirect_stdout(output):
|
|
2761
|
+
if target == 1:
|
|
2762
|
+
jsonpatch_cli.diff_files()
|
|
2763
|
+
self.assertEqual(output.getvalue(), '')
|
|
2764
|
+
else:
|
|
2765
|
+
with self.assertRaises(SystemExit) as caught:
|
|
2766
|
+
jsonpatch_cli.diff_files()
|
|
2767
|
+
self.assertEqual(caught.exception.code, 1)
|
|
2768
|
+
self.assertEqual(jsonpatch.apply_patch({'a': 1}, json.loads(output.getvalue())),
|
|
2769
|
+
{'a': target})
|
|
2770
|
+
self.assertTrue(args.FILE1.closed)
|
|
2771
|
+
self.assertTrue(args.FILE2.closed)
|
|
2772
|
+
|
|
2773
|
+
def test_jsondiff_closes_both_inputs_after_parse_error(self):
|
|
2774
|
+
for invalid in (0, 1):
|
|
2775
|
+
with self.subTest(invalid=invalid):
|
|
2776
|
+
paths = [self.write('a.json', {}), self.write('b.json', {})]
|
|
2777
|
+
with open(paths[invalid], 'w', encoding='utf-8') as f:
|
|
2778
|
+
f.write('{')
|
|
2779
|
+
parser = jsonpatch_cli.jsondiff_parser
|
|
2780
|
+
args = parser.parse_args(paths)
|
|
2781
|
+
self.addCleanup(args.FILE1.close)
|
|
2782
|
+
self.addCleanup(args.FILE2.close)
|
|
2783
|
+
with mock.patch.object(parser, 'parse_args', return_value=args):
|
|
2784
|
+
with self.assertRaises(json.JSONDecodeError):
|
|
2785
|
+
jsonpatch_cli.diff_files()
|
|
2786
|
+
self.assertTrue(args.FILE1.closed)
|
|
2787
|
+
self.assertTrue(args.FILE2.closed)
|
|
2788
|
+
|
|
2789
|
+
def test_jsondiff_keeps_stdin_open(self):
|
|
2790
|
+
for stdin_first in (False, True):
|
|
2791
|
+
with self.subTest(stdin_first=stdin_first):
|
|
2792
|
+
path = self.write('a.json', {'a': 1})
|
|
2793
|
+
stdin = io.StringIO('{"a": 1}')
|
|
2794
|
+
self.addCleanup(stdin.close)
|
|
2795
|
+
parser = jsonpatch_cli.jsondiff_parser
|
|
2796
|
+
with mock.patch.object(sys, 'stdin', stdin):
|
|
2797
|
+
args = parser.parse_args(['-', path] if stdin_first else [path, '-'])
|
|
2798
|
+
owned = args.FILE2 if stdin_first else args.FILE1
|
|
2799
|
+
self.addCleanup(owned.close)
|
|
2800
|
+
with mock.patch.object(parser, 'parse_args', return_value=args), \
|
|
2801
|
+
contextlib.redirect_stdout(io.StringIO()):
|
|
2802
|
+
jsonpatch_cli.diff_files()
|
|
2803
|
+
self.assertFalse(stdin.closed)
|
|
2804
|
+
self.assertTrue(owned.closed)
|
|
2805
|
+
|
|
2714
2806
|
def test_jsondiff_indent(self):
|
|
2715
2807
|
src = self.write('a.json', {'a': 1})
|
|
2716
2808
|
dst = self.write('b.json', {'b': 1})
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|