jsonpatch 1.42__tar.gz → 1.44__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.44}/PKG-INFO +1 -1
- {jsonpatch-1.42 → jsonpatch-1.44}/jsonpatch-stubs/__init__.pyi +6 -5
- {jsonpatch-1.42 → jsonpatch-1.44/jsonpatch.egg-info}/PKG-INFO +1 -1
- {jsonpatch-1.42 → jsonpatch-1.44}/jsonpatch.py +13 -5
- {jsonpatch-1.42 → jsonpatch-1.44}/jsonpatch_cli.py +7 -2
- {jsonpatch-1.42 → jsonpatch-1.44}/tests.py +121 -3
- {jsonpatch-1.42 → jsonpatch-1.44}/typetests/usage.py +10 -1
- {jsonpatch-1.42 → jsonpatch-1.44}/AUTHORS +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/LICENSE +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/MANIFEST.in +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/README.md +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/doc/commandline.rst +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/doc/index.rst +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/doc/mod-jsonpatch.rst +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/doc/tutorial.rst +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/ext_tests.py +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/jsonpatch.egg-info/SOURCES.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/jsonpatch.egg-info/dependency_links.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/jsonpatch.egg-info/entry_points.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/jsonpatch.egg-info/requires.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/jsonpatch.egg-info/top_level.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/property_tests.py +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/pyproject.toml +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/pytest.ini +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/requirements.txt +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/setup.cfg +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/setup.py +0 -0
- {jsonpatch-1.42 → jsonpatch-1.44}/tests.js +0 -0
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
# also accepts arbitrary Mapping / Sequence types, which a recursive JSON type
|
|
10
10
|
# would reject.
|
|
11
11
|
|
|
12
|
-
from collections.abc import Callable, Iterable, Iterator, Mapping
|
|
12
|
+
from collections.abc import Callable, Iterable, Iterator, Mapping
|
|
13
|
+
from types import MappingProxyType
|
|
13
14
|
from typing import Any, ClassVar
|
|
14
15
|
|
|
15
16
|
# Re-exported, as jsonpatch.py imports them and users rely on that, e.g. to
|
|
@@ -57,7 +58,7 @@ class PatchOperation:
|
|
|
57
58
|
pointer_cls: type[JsonPointer]
|
|
58
59
|
location: str
|
|
59
60
|
pointer: JsonPointer
|
|
60
|
-
operation:
|
|
61
|
+
operation: dict[str, Any]
|
|
61
62
|
def __init__(self, operation: _Operation, pointer_cls: type[JsonPointer] = ...) -> None: ...
|
|
62
63
|
def apply(self, obj: Any) -> Any: ...
|
|
63
64
|
def __hash__(self) -> int: ...
|
|
@@ -92,12 +93,12 @@ class CopyOperation(PatchOperation): ...
|
|
|
92
93
|
class JsonPatch:
|
|
93
94
|
json_dumper: ClassVar[Callable[..., str]]
|
|
94
95
|
json_loader: ClassVar[Callable[..., Any]]
|
|
95
|
-
operations: ClassVar[
|
|
96
|
+
operations: ClassVar[MappingProxyType[str, type[PatchOperation]]]
|
|
96
97
|
pointer_cls: type[JsonPointer]
|
|
97
|
-
patch:
|
|
98
|
+
patch: list[dict[str, Any]]
|
|
98
99
|
def __init__(self, patch: Iterable[_Operation], pointer_cls: type[JsonPointer] = ...) -> None: ...
|
|
99
100
|
def __bool__(self) -> bool: ...
|
|
100
|
-
def __iter__(self) -> Iterator[
|
|
101
|
+
def __iter__(self) -> Iterator[dict[str, Any]]: ...
|
|
101
102
|
def __hash__(self) -> int: ...
|
|
102
103
|
def __eq__(self, other: object) -> bool: ...
|
|
103
104
|
def __ne__(self, other: object) -> bool: ...
|
|
@@ -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.44'
|
|
59
59
|
__website__ = 'https://github.com/stefankoegl/python-json-patch'
|
|
60
60
|
__license__ = 'BSD-3-Clause'
|
|
61
61
|
|
|
@@ -373,7 +373,8 @@ class PatchOperation(object):
|
|
|
373
373
|
except TypeError:
|
|
374
374
|
raise InvalidJsonPatch("Invalid 'path'", op=operation)
|
|
375
375
|
|
|
376
|
-
|
|
376
|
+
# Setters update operation members, even when the input is read-only.
|
|
377
|
+
self.operation = operation if isinstance(operation, dict) else dict(operation)
|
|
377
378
|
|
|
378
379
|
def _from_pointer(self):
|
|
379
380
|
"""Builds the pointer of the 'from' member like __init__ builds the
|
|
@@ -791,7 +792,7 @@ class JsonPatch(object):
|
|
|
791
792
|
if isinstance(patch, (str, bytes, Mapping)):
|
|
792
793
|
raise InvalidJsonPatch("Document is expected to be sequence of operations")
|
|
793
794
|
try:
|
|
794
|
-
self.patch =
|
|
795
|
+
self.patch = list(patch)
|
|
795
796
|
except TypeError:
|
|
796
797
|
raise InvalidJsonPatch("Document is expected to be sequence of operations")
|
|
797
798
|
|
|
@@ -799,7 +800,7 @@ class JsonPatch(object):
|
|
|
799
800
|
# is correct by retrieving each patch element.
|
|
800
801
|
# Much of the validation is done in the initializer
|
|
801
802
|
# though some is delayed until the patch is applied.
|
|
802
|
-
for op in self.patch:
|
|
803
|
+
for index, op in enumerate(self.patch):
|
|
803
804
|
# We're only checking for strings in the following check
|
|
804
805
|
# for two reasons:
|
|
805
806
|
#
|
|
@@ -815,6 +816,10 @@ class JsonPatch(object):
|
|
|
815
816
|
op=op)
|
|
816
817
|
|
|
817
818
|
self._get_operation(op)
|
|
819
|
+
# Keep validation errors tied to the original input, but expose
|
|
820
|
+
# concrete, JSON-serializable operation dictionaries on success.
|
|
821
|
+
if not isinstance(op, dict):
|
|
822
|
+
self.patch[index] = dict(op)
|
|
818
823
|
|
|
819
824
|
def __str__(self):
|
|
820
825
|
"""str(self) -> self.to_string()"""
|
|
@@ -869,6 +874,9 @@ class JsonPatch(object):
|
|
|
869
874
|
one. The resulting patch transforms `src` into `dst`, so it has to be
|
|
870
875
|
applied to `src` (or to a document equal to it).
|
|
871
876
|
|
|
877
|
+
Values in the patch are copied, so changing either input document
|
|
878
|
+
afterwards does not change the generated patch.
|
|
879
|
+
|
|
872
880
|
:param src: Source JSON document, including arrays and scalar values.
|
|
873
881
|
:type src: object
|
|
874
882
|
|
|
@@ -906,7 +914,7 @@ class JsonPatch(object):
|
|
|
906
914
|
optimization=optimization,
|
|
907
915
|
generate_test_ops=generate_test_ops)
|
|
908
916
|
builder._compare_values((), None, src, dst)
|
|
909
|
-
ops = list(builder.execute())
|
|
917
|
+
ops = copy.deepcopy(list(builder.execute()))
|
|
910
918
|
return cls(ops, pointer_cls=pointer_cls)
|
|
911
919
|
|
|
912
920
|
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,
|
|
@@ -1783,9 +1818,35 @@ class JsonPatchCreationTest(unittest.TestCase):
|
|
|
1783
1818
|
({'op': 'add', 'path': '/a', 'value': 1},)):
|
|
1784
1819
|
with self.subTest(operations=operations):
|
|
1785
1820
|
patch = jsonpatch.JsonPatch(operations)
|
|
1786
|
-
self.
|
|
1821
|
+
self.assertIsInstance(patch.patch, list)
|
|
1822
|
+
self.assertEqual(patch.patch, list(operations))
|
|
1787
1823
|
self.assertEqual(patch.apply({}), {'a': 1} if operations else {})
|
|
1788
1824
|
|
|
1825
|
+
def test_creation_normalizes_abstract_operations(self):
|
|
1826
|
+
operation = MappingProxyType({'op': 'add', 'path': '/a', 'value': 1})
|
|
1827
|
+
for operations in ([operation], (operation,), iter([operation])):
|
|
1828
|
+
with self.subTest(operations=type(operations).__name__):
|
|
1829
|
+
patch = jsonpatch.JsonPatch(operations)
|
|
1830
|
+
self.assertIsInstance(patch.patch, list)
|
|
1831
|
+
self.assertIsInstance(patch.patch[0], dict)
|
|
1832
|
+
self.assertIsInstance(next(iter(patch)), dict)
|
|
1833
|
+
self.assertEqual(json.loads(patch.to_string()), [dict(operation)])
|
|
1834
|
+
patch.patch[0]['path'] = '/b'
|
|
1835
|
+
self.assertEqual(patch.apply({}), {'b': 1})
|
|
1836
|
+
self.assertEqual(operation['path'], '/a')
|
|
1837
|
+
|
|
1838
|
+
def test_operation_normalizes_read_only_mapping(self):
|
|
1839
|
+
operation = MappingProxyType({
|
|
1840
|
+
'op': 'move', 'from': '/a/0', 'path': '/b/0'})
|
|
1841
|
+
move = jsonpatch.MoveOperation(operation)
|
|
1842
|
+
self.assertIsInstance(move.operation, dict)
|
|
1843
|
+
move.key = 1
|
|
1844
|
+
move.from_key = 2
|
|
1845
|
+
self.assertEqual(move.operation['path'], '/b/1')
|
|
1846
|
+
self.assertEqual(move.operation['from'], '/a/2')
|
|
1847
|
+
self.assertEqual(operation['path'], '/b/0')
|
|
1848
|
+
self.assertEqual(operation['from'], '/a/0')
|
|
1849
|
+
|
|
1789
1850
|
def test_creation_fails_with_invalid_patch(self):
|
|
1790
1851
|
invalid_patches = [
|
|
1791
1852
|
{ 'path': '/foo', 'value': 'bar'},
|
|
@@ -2711,6 +2772,63 @@ class CommandLineTests(ScriptRunner, unittest.TestCase):
|
|
|
2711
2772
|
res = self.run_script('jsondiff', src, dst)
|
|
2712
2773
|
self.assertEqual((res.returncode, res.stdout), (0, ''), res.stderr)
|
|
2713
2774
|
|
|
2775
|
+
def test_jsondiff_closes_inputs(self):
|
|
2776
|
+
for target in (1, 2):
|
|
2777
|
+
with self.subTest(target=target):
|
|
2778
|
+
src = self.write('a.json', {'a': 1})
|
|
2779
|
+
dst = self.write('b.json', {'a': target})
|
|
2780
|
+
parser = jsonpatch_cli.jsondiff_parser
|
|
2781
|
+
args = parser.parse_args([src, dst])
|
|
2782
|
+
self.addCleanup(args.FILE1.close)
|
|
2783
|
+
self.addCleanup(args.FILE2.close)
|
|
2784
|
+
output = io.StringIO()
|
|
2785
|
+
with mock.patch.object(parser, 'parse_args', return_value=args), \
|
|
2786
|
+
contextlib.redirect_stdout(output):
|
|
2787
|
+
if target == 1:
|
|
2788
|
+
jsonpatch_cli.diff_files()
|
|
2789
|
+
self.assertEqual(output.getvalue(), '')
|
|
2790
|
+
else:
|
|
2791
|
+
with self.assertRaises(SystemExit) as caught:
|
|
2792
|
+
jsonpatch_cli.diff_files()
|
|
2793
|
+
self.assertEqual(caught.exception.code, 1)
|
|
2794
|
+
self.assertEqual(jsonpatch.apply_patch({'a': 1}, json.loads(output.getvalue())),
|
|
2795
|
+
{'a': target})
|
|
2796
|
+
self.assertTrue(args.FILE1.closed)
|
|
2797
|
+
self.assertTrue(args.FILE2.closed)
|
|
2798
|
+
|
|
2799
|
+
def test_jsondiff_closes_both_inputs_after_parse_error(self):
|
|
2800
|
+
for invalid in (0, 1):
|
|
2801
|
+
with self.subTest(invalid=invalid):
|
|
2802
|
+
paths = [self.write('a.json', {}), self.write('b.json', {})]
|
|
2803
|
+
with open(paths[invalid], 'w', encoding='utf-8') as f:
|
|
2804
|
+
f.write('{')
|
|
2805
|
+
parser = jsonpatch_cli.jsondiff_parser
|
|
2806
|
+
args = parser.parse_args(paths)
|
|
2807
|
+
self.addCleanup(args.FILE1.close)
|
|
2808
|
+
self.addCleanup(args.FILE2.close)
|
|
2809
|
+
with mock.patch.object(parser, 'parse_args', return_value=args):
|
|
2810
|
+
with self.assertRaises(json.JSONDecodeError):
|
|
2811
|
+
jsonpatch_cli.diff_files()
|
|
2812
|
+
self.assertTrue(args.FILE1.closed)
|
|
2813
|
+
self.assertTrue(args.FILE2.closed)
|
|
2814
|
+
|
|
2815
|
+
def test_jsondiff_keeps_stdin_open(self):
|
|
2816
|
+
for stdin_first in (False, True):
|
|
2817
|
+
with self.subTest(stdin_first=stdin_first):
|
|
2818
|
+
path = self.write('a.json', {'a': 1})
|
|
2819
|
+
stdin = io.StringIO('{"a": 1}')
|
|
2820
|
+
self.addCleanup(stdin.close)
|
|
2821
|
+
parser = jsonpatch_cli.jsondiff_parser
|
|
2822
|
+
with mock.patch.object(sys, 'stdin', stdin):
|
|
2823
|
+
args = parser.parse_args(['-', path] if stdin_first else [path, '-'])
|
|
2824
|
+
owned = args.FILE2 if stdin_first else args.FILE1
|
|
2825
|
+
self.addCleanup(owned.close)
|
|
2826
|
+
with mock.patch.object(parser, 'parse_args', return_value=args), \
|
|
2827
|
+
contextlib.redirect_stdout(io.StringIO()):
|
|
2828
|
+
jsonpatch_cli.diff_files()
|
|
2829
|
+
self.assertFalse(stdin.closed)
|
|
2830
|
+
self.assertTrue(owned.closed)
|
|
2831
|
+
|
|
2714
2832
|
def test_jsondiff_indent(self):
|
|
2715
2833
|
src = self.write('a.json', {'a': 1})
|
|
2716
2834
|
dst = self.write('b.json', {'b': 1})
|
|
@@ -9,6 +9,7 @@ unused ignores), so they test that wrong usage is rejected.
|
|
|
9
9
|
|
|
10
10
|
import json
|
|
11
11
|
from collections.abc import Mapping
|
|
12
|
+
from types import MappingProxyType
|
|
12
13
|
from typing import Any
|
|
13
14
|
|
|
14
15
|
import jsonpointer
|
|
@@ -45,12 +46,19 @@ assert_type(patch.apply(doc, in_place=True, atomic=True), Any)
|
|
|
45
46
|
assert_type(patch.to_string(), str)
|
|
46
47
|
assert_type(patch.to_string(dumps=json.dumps), str)
|
|
47
48
|
assert_type(bool(patch), bool)
|
|
48
|
-
assert_type(
|
|
49
|
+
assert_type(patch.patch, list[dict[str, Any]])
|
|
50
|
+
assert_type(list(patch), list[dict[str, Any]])
|
|
51
|
+
assert_type(next(iter(patch)), dict[str, Any])
|
|
52
|
+
assert_type(jsonpatch.make_patch(doc, {}).patch, list[dict[str, Any]])
|
|
53
|
+
abstract_op: Mapping[str, str] = MappingProxyType({'op': 'remove', 'path': '/foo'})
|
|
54
|
+
JsonPatch(iter((abstract_op,)))
|
|
55
|
+
assert_type(PatchOperation(abstract_op).operation, dict[str, Any])
|
|
49
56
|
assert_type(JsonPatch.from_string('[]'), JsonPatch)
|
|
50
57
|
assert_type(JsonPatch.from_string(b'[]', loads=json.loads), JsonPatch)
|
|
51
58
|
assert_type(JsonPatch.from_string(bytearray(b'[]')), JsonPatch)
|
|
52
59
|
assert_type(JsonPatch.from_diff(doc, {}, optimization=False), JsonPatch)
|
|
53
60
|
assert_type(JsonPatch.operations['add'], type[PatchOperation])
|
|
61
|
+
assert_type(JsonPatch.operations, MappingProxyType[str, type[PatchOperation]])
|
|
54
62
|
|
|
55
63
|
|
|
56
64
|
class MyPatch(JsonPatch):
|
|
@@ -69,6 +77,7 @@ assert_type(operation.path, str)
|
|
|
69
77
|
assert_type(operation.key, int | str)
|
|
70
78
|
operation.key = 1
|
|
71
79
|
assert_type(operation.pointer, JsonPointer)
|
|
80
|
+
assert_type(operation.operation, dict[str, Any])
|
|
72
81
|
move = jsonpatch.MoveOperation({'op': 'move', 'from': '/a', 'path': '/b'})
|
|
73
82
|
assert_type(move.from_key, int | str)
|
|
74
83
|
|
|
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
|