jsonpatch 1.39__tar.gz → 1.40__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.
Files changed (26) hide show
  1. {jsonpatch-1.39/jsonpatch.egg-info → jsonpatch-1.40}/PKG-INFO +1 -1
  2. {jsonpatch-1.39 → jsonpatch-1.40}/doc/commandline.rst +3 -1
  3. {jsonpatch-1.39 → jsonpatch-1.40}/doc/tutorial.rst +19 -0
  4. {jsonpatch-1.39 → jsonpatch-1.40/jsonpatch.egg-info}/PKG-INFO +1 -1
  5. {jsonpatch-1.39 → jsonpatch-1.40}/jsonpatch.py +111 -49
  6. {jsonpatch-1.39 → jsonpatch-1.40}/jsonpatch_cli.py +5 -3
  7. {jsonpatch-1.39 → jsonpatch-1.40}/property_tests.py +19 -0
  8. {jsonpatch-1.39 → jsonpatch-1.40}/tests.py +222 -0
  9. {jsonpatch-1.39 → jsonpatch-1.40}/AUTHORS +0 -0
  10. {jsonpatch-1.39 → jsonpatch-1.40}/LICENSE +0 -0
  11. {jsonpatch-1.39 → jsonpatch-1.40}/MANIFEST.in +0 -0
  12. {jsonpatch-1.39 → jsonpatch-1.40}/README.md +0 -0
  13. {jsonpatch-1.39 → jsonpatch-1.40}/doc/index.rst +0 -0
  14. {jsonpatch-1.39 → jsonpatch-1.40}/doc/mod-jsonpatch.rst +0 -0
  15. {jsonpatch-1.39 → jsonpatch-1.40}/ext_tests.py +0 -0
  16. {jsonpatch-1.39 → jsonpatch-1.40}/jsonpatch.egg-info/SOURCES.txt +0 -0
  17. {jsonpatch-1.39 → jsonpatch-1.40}/jsonpatch.egg-info/dependency_links.txt +0 -0
  18. {jsonpatch-1.39 → jsonpatch-1.40}/jsonpatch.egg-info/entry_points.txt +0 -0
  19. {jsonpatch-1.39 → jsonpatch-1.40}/jsonpatch.egg-info/requires.txt +0 -0
  20. {jsonpatch-1.39 → jsonpatch-1.40}/jsonpatch.egg-info/top_level.txt +0 -0
  21. {jsonpatch-1.39 → jsonpatch-1.40}/pyproject.toml +0 -0
  22. {jsonpatch-1.39 → jsonpatch-1.40}/pytest.ini +0 -0
  23. {jsonpatch-1.39 → jsonpatch-1.40}/requirements.txt +0 -0
  24. {jsonpatch-1.39 → jsonpatch-1.40}/setup.cfg +0 -0
  25. {jsonpatch-1.39 → jsonpatch-1.40}/setup.py +0 -0
  26. {jsonpatch-1.39 → jsonpatch-1.40}/tests.js +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jsonpatch
3
- Version: 1.39
3
+ Version: 1.40
4
4
  Summary: Apply JSON-Patches (RFC 6902)
5
5
  Home-page: https://github.com/stefankoegl/python-json-patch
6
6
  Author: Stefan Kögl
@@ -13,7 +13,7 @@ in-place, it also writes UTF-8.
13
13
  The program ``jsondiff`` can be used to create a JSON patch by comparing two
14
14
  JSON files ::
15
15
 
16
- usage: jsondiff [-h] [--indent INDENT] [-u] [-v] FILE1 FILE2
16
+ usage: jsondiff [-h] [--indent INDENT] [-u] [-t] [-v] FILE1 FILE2
17
17
 
18
18
  Diff two JSON files
19
19
 
@@ -27,6 +27,8 @@ JSON files ::
27
27
  -u, --preserve-unicode
28
28
  Output Unicode character as-is without using Code
29
29
  Point
30
+ -t, --test-ops Precede each remove and replace operation by a test of
31
+ the value it changes
30
32
  -v, --version show program's version number and exit
31
33
 
32
34
  Example
@@ -79,6 +79,25 @@ Applying it to any other object, such as ``dst``, usually fails with a
79
79
  ...
80
80
  jsonpatch.JsonPatchConflict: can't remove a non-existent object 'foo'
81
81
 
82
+ Applying it to an object with the same structure but different values may
83
+ succeed, though, and overwrite those values. With ``generate_test_ops=True``
84
+ each ``remove`` and ``replace`` operation is preceded by a ``test`` operation
85
+ that checks the value it removes or replaces. The patch then also records the
86
+ previous values, and fails with a ``JsonPatchTestFailed`` if they differ:
87
+
88
+ .. code-block:: python
89
+
90
+ >>> patch = jsonpatch.make_patch({'a': 1, 'b': 2}, {'a': 3},
91
+ ... generate_test_ops=True)
92
+ >>> patch.patch # doctest: +NORMALIZE_WHITESPACE
93
+ [{'op': 'test', 'path': '/b', 'value': 2}, {'op': 'remove', 'path': '/b'},
94
+ {'op': 'test', 'path': '/a', 'value': 1},
95
+ {'op': 'replace', 'path': '/a', 'value': 3}]
96
+ >>> patch.apply({'a': 5, 'b': 2})
97
+ Traceback (most recent call last):
98
+ ...
99
+ jsonpatch.JsonPatchTestFailed: 5 (<class 'int'>) is not equal to tested value 1 (<class 'int'>)
100
+
82
101
  If a patch is only used once, it is not necessary to create a patch object
83
102
  explicitly.
84
103
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jsonpatch
3
- Version: 1.39
3
+ Version: 1.40
4
4
  Summary: Apply JSON-Patches (RFC 6902)
5
5
  Home-page: https://github.com/stefankoegl/python-json-patch
6
6
  Author: Stefan Kögl
@@ -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.39'
58
+ __version__ = '1.40'
59
59
  __website__ = 'https://github.com/stefankoegl/python-json-patch'
60
60
  __license__ = 'BSD-3-Clause'
61
61
 
@@ -63,6 +63,19 @@ __license__ = 'BSD-3-Clause'
63
63
  class JsonPatchException(Exception):
64
64
  """Base Json Patch exception"""
65
65
 
66
+ def __init__(self, *args, **kwargs):
67
+ """Initialize a new instance of a JsonPatchException
68
+
69
+ :param op: The operation that failed. It is attached to the exception
70
+ so that callers can inspect (or modify) the failing patch
71
+ operation. This is either a :class:`PatchOperation`
72
+ instance or, if the operation could not be constructed,
73
+ the raw operation object.
74
+ :type op: PatchOperation or dict or None
75
+ """
76
+ self.op = kwargs.pop('op', None)
77
+ super(JsonPatchException, self).__init__(*args, **kwargs)
78
+
66
79
 
67
80
  class InvalidJsonPatch(JsonPatchException):
68
81
  """ Raised if an invalid JSON Patch is created """
@@ -140,7 +153,7 @@ def apply_patch(doc, patch, in_place=False, pointer_cls=JsonPointer):
140
153
  return patch.apply(doc, in_place)
141
154
 
142
155
 
143
- def make_patch(src, dst, pointer_cls=JsonPointer):
156
+ def make_patch(src, dst, pointer_cls=JsonPointer, generate_test_ops=False):
144
157
  """Generates patch by comparing two document objects. Actually is
145
158
  a proxy to :meth:`JsonPatch.from_diff` method. The resulting patch
146
159
  transforms `src` into `dst`, so it has to be applied to `src` (or to a
@@ -155,6 +168,12 @@ def make_patch(src, dst, pointer_cls=JsonPointer):
155
168
  :param pointer_cls: JSON pointer class to use.
156
169
  :type pointer_cls: Type[JsonPointer]
157
170
 
171
+ :param generate_test_ops: Precede each ``remove`` and ``replace``
172
+ operation by a ``test`` operation that checks
173
+ the value it removes or replaces, so the patch
174
+ fails on documents that differ from `src` there.
175
+ :type generate_test_ops: bool
176
+
158
177
  >>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
159
178
  >>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]}
160
179
  >>> patch = make_patch(src, dst)
@@ -163,7 +182,8 @@ def make_patch(src, dst, pointer_cls=JsonPointer):
163
182
  True
164
183
  """
165
184
 
166
- return JsonPatch.from_diff(src, dst, pointer_cls=pointer_cls)
185
+ return JsonPatch.from_diff(src, dst, pointer_cls=pointer_cls,
186
+ generate_test_ops=generate_test_ops)
167
187
 
168
188
 
169
189
  class PatchOperation(object):
@@ -173,7 +193,7 @@ class PatchOperation(object):
173
193
  self.pointer_cls = pointer_cls
174
194
 
175
195
  if not operation.__contains__('path'):
176
- raise InvalidJsonPatch("Operation must have a 'path' member")
196
+ raise InvalidJsonPatch("Operation must have a 'path' member", op=operation)
177
197
 
178
198
  if isinstance(operation['path'], self.pointer_cls):
179
199
  self.location = operation['path'].path
@@ -183,7 +203,7 @@ class PatchOperation(object):
183
203
  try:
184
204
  self.pointer = self.pointer_cls(self.location)
185
205
  except TypeError:
186
- raise InvalidJsonPatch("Invalid 'path'")
206
+ raise InvalidJsonPatch("Invalid 'path'", op=operation)
187
207
 
188
208
  self.operation = operation
189
209
 
@@ -194,7 +214,7 @@ class PatchOperation(object):
194
214
  from_ptr = self.operation['from']
195
215
  except KeyError:
196
216
  raise InvalidJsonPatch(
197
- "The operation does not contain a 'from' member")
217
+ "The operation does not contain a 'from' member", op=self)
198
218
 
199
219
  if isinstance(from_ptr, self.pointer_cls):
200
220
  return from_ptr
@@ -202,7 +222,7 @@ class PatchOperation(object):
202
222
  try:
203
223
  return self.pointer_cls(from_ptr)
204
224
  except TypeError:
205
- raise InvalidJsonPatch("Invalid 'from'")
225
+ raise InvalidJsonPatch("Invalid 'from'", op=self)
206
226
 
207
227
  def apply(self, obj):
208
228
  """Abstract method that applies a patch operation to the specified object."""
@@ -253,7 +273,7 @@ class RemoveOperation(PatchOperation):
253
273
  subobj, part = self.pointer.to_last(obj)
254
274
 
255
275
  if part is None:
256
- raise JsonPatchConflict("can't remove the whole document")
276
+ raise JsonPatchConflict("can't remove the whole document", op=self)
257
277
 
258
278
  if isinstance(subobj, Sequence) and not isinstance(part, int):
259
279
  raise JsonPointerException("invalid array index '{0}'".format(part))
@@ -262,9 +282,9 @@ class RemoveOperation(PatchOperation):
262
282
  del subobj[part]
263
283
  except (KeyError, IndexError):
264
284
  msg = "can't remove a non-existent object '{0}'".format(part)
265
- raise JsonPatchConflict(msg)
285
+ raise JsonPatchConflict(msg, op=self)
266
286
  except TypeError as ex:
267
- raise JsonPatchConflict(str(ex))
287
+ raise JsonPatchConflict(str(ex), op=self)
268
288
 
269
289
  return obj
270
290
 
@@ -277,7 +297,7 @@ class AddOperation(PatchOperation):
277
297
  value = self.operation["value"]
278
298
  except KeyError:
279
299
  raise InvalidJsonPatch(
280
- "The operation does not contain a 'value' member")
300
+ "The operation does not contain a 'value' member", op=self)
281
301
 
282
302
  # Insert a copy so the document does not share mutable values with
283
303
  # the patch; otherwise later operations would modify the patch itself.
@@ -294,7 +314,7 @@ class AddOperation(PatchOperation):
294
314
  subobj.append(value) # pylint: disable=E1103
295
315
 
296
316
  elif part > len(subobj) or part < 0:
297
- raise JsonPatchConflict("can't insert outside of list")
317
+ raise JsonPatchConflict("can't insert outside of list", op=self)
298
318
 
299
319
  else:
300
320
  subobj.insert(part, value) # pylint: disable=E1103
@@ -303,7 +323,7 @@ class AddOperation(PatchOperation):
303
323
  subobj[part] = value
304
324
 
305
325
  else:
306
- raise JsonPatchConflict("unable to fully resolve json pointer {0}, part {1}".format(self.location, part))
326
+ raise JsonPatchConflict("unable to fully resolve json pointer {0}, part {1}".format(self.location, part), op=self)
307
327
  return obj
308
328
 
309
329
 
@@ -315,7 +335,7 @@ class ReplaceOperation(PatchOperation):
315
335
  value = self.operation["value"]
316
336
  except KeyError:
317
337
  raise InvalidJsonPatch(
318
- "The operation does not contain a 'value' member")
338
+ "The operation does not contain a 'value' member", op=self)
319
339
 
320
340
  # copied for the same reason as in AddOperation.apply
321
341
  value = copy.deepcopy(value)
@@ -329,17 +349,17 @@ class ReplaceOperation(PatchOperation):
329
349
  # '-' only refers to the (nonexistent) element after the end of
330
350
  # an array; for an object it is an ordinary member name
331
351
  if part == "-":
332
- raise InvalidJsonPatch("'path' with '-' can't be applied to 'replace' operation")
352
+ raise InvalidJsonPatch("'path' with '-' can't be applied to 'replace' operation", op=self)
333
353
 
334
354
  if part >= len(subobj) or part < 0:
335
- raise JsonPatchConflict("can't replace outside of list")
355
+ raise JsonPatchConflict("can't replace outside of list", op=self)
336
356
 
337
357
  elif isinstance(subobj, MutableMapping):
338
358
  if part not in subobj:
339
359
  msg = "can't replace a non-existent object '{0}'".format(part)
340
- raise JsonPatchConflict(msg)
360
+ raise JsonPatchConflict(msg, op=self)
341
361
  else:
342
- raise JsonPatchConflict("unable to fully resolve json pointer {0}, part {1}".format(self.location, part))
362
+ raise JsonPatchConflict("unable to fully resolve json pointer {0}, part {1}".format(self.location, part), op=self)
343
363
 
344
364
  subobj[part] = value
345
365
  return obj
@@ -355,7 +375,7 @@ class MoveOperation(PatchOperation):
355
375
  # shifts its siblings, so the target would resolve to a different
356
376
  # location. This also covers moving the whole document ('from' is "").
357
377
  if self.pointer != from_ptr and self.pointer.contains(from_ptr):
358
- raise JsonPatchConflict('Cannot move values into their own children')
378
+ raise JsonPatchConflict('Cannot move values into their own children', op=self)
359
379
 
360
380
  subobj, part = _to_existing_last(from_ptr, obj)
361
381
 
@@ -366,22 +386,29 @@ class MoveOperation(PatchOperation):
366
386
  try:
367
387
  value = subobj[part]
368
388
  except (KeyError, IndexError) as ex:
369
- raise JsonPatchConflict(str(ex))
389
+ raise JsonPatchConflict(str(ex), op=self)
370
390
 
371
391
  # If source and target are equal, this is a no-op
372
392
  if self.pointer == from_ptr:
373
393
  return obj
374
394
 
375
- obj = RemoveOperation({
376
- 'op': 'remove',
377
- 'path': self.operation['from']
378
- }, pointer_cls=self.pointer_cls).apply(obj)
379
-
380
- # the value has been detached from its old location, so no copy needed
381
- obj = AddOperation({
382
- 'op': 'add',
383
- 'path': self.location,
384
- }, pointer_cls=self.pointer_cls)._add(obj, value)
395
+ try:
396
+ obj = RemoveOperation({
397
+ 'op': 'remove',
398
+ 'path': self.operation['from']
399
+ }, pointer_cls=self.pointer_cls).apply(obj)
400
+
401
+ # the value has been detached from its old location, so no copy
402
+ # needed
403
+ obj = AddOperation({
404
+ 'op': 'add',
405
+ 'path': self.location,
406
+ }, pointer_cls=self.pointer_cls)._add(obj, value)
407
+ except JsonPatchException as ex:
408
+ # report the failure against this operation rather than the
409
+ # internal, synthetic remove / add operations
410
+ ex.op = self
411
+ raise
385
412
 
386
413
  return obj
387
414
 
@@ -422,18 +449,18 @@ class TestOperation(PatchOperation):
422
449
  else:
423
450
  val = self.pointer.walk(subobj, part)
424
451
  except JsonPointerException as ex:
425
- raise JsonPatchTestFailed(str(ex))
452
+ raise JsonPatchTestFailed(str(ex), op=self)
426
453
 
427
454
  try:
428
455
  value = self.operation['value']
429
456
  except KeyError:
430
457
  raise InvalidJsonPatch(
431
- "The operation does not contain a 'value' member")
458
+ "The operation does not contain a 'value' member", op=self)
432
459
 
433
460
  if not _json_equal(val, value):
434
461
  msg = '{0} ({1}) is not equal to tested value {2} ({3})'
435
462
  raise JsonPatchTestFailed(msg.format(val, type(val),
436
- value, type(value)))
463
+ value, type(value)), op=self)
437
464
 
438
465
  return obj
439
466
 
@@ -448,13 +475,19 @@ class CopyOperation(PatchOperation):
448
475
  try:
449
476
  value = copy.deepcopy(subobj if part is None else subobj[part])
450
477
  except (KeyError, IndexError) as ex:
451
- raise JsonPatchConflict(str(ex))
478
+ raise JsonPatchConflict(str(ex), op=self)
452
479
 
453
480
  # value is already a deep copy, no need to copy it again
454
- obj = AddOperation({
455
- 'op': 'add',
456
- 'path': self.location,
457
- }, pointer_cls=self.pointer_cls)._add(obj, value)
481
+ try:
482
+ obj = AddOperation({
483
+ 'op': 'add',
484
+ 'path': self.location,
485
+ }, pointer_cls=self.pointer_cls)._add(obj, value)
486
+ except JsonPatchException as ex:
487
+ # report the failure against this operation rather than the
488
+ # internal, synthetic add operation
489
+ ex.op = self
490
+ raise
458
491
 
459
492
  return obj
460
493
 
@@ -547,7 +580,8 @@ class JsonPatch(object):
547
580
  # of mappings, this won't raise.
548
581
  if isinstance(op, (str, bytes)):
549
582
  raise InvalidJsonPatch("Document is expected to be sequence of "
550
- "operations, got a sequence of strings.")
583
+ "operations, got a sequence of strings.",
584
+ op=op)
551
585
 
552
586
  self._get_operation(op)
553
587
 
@@ -597,7 +631,7 @@ class JsonPatch(object):
597
631
  @classmethod
598
632
  def from_diff(
599
633
  cls, src, dst, optimization=True, dumps=None,
600
- pointer_cls=JsonPointer,
634
+ pointer_cls=JsonPointer, generate_test_ops=False,
601
635
  ):
602
636
  """Creates JsonPatch instance based on comparison of two document
603
637
  objects. Json patch would be created for `src` argument against `dst`
@@ -621,6 +655,12 @@ class JsonPatch(object):
621
655
  :param pointer_cls: JSON pointer class to use.
622
656
  :type pointer_cls: Type[JsonPointer]
623
657
 
658
+ :param generate_test_ops: Precede each ``remove`` and ``replace``
659
+ operation by a ``test`` operation that checks
660
+ the value it removes or replaces, so the patch
661
+ fails on documents that differ from `src` there.
662
+ :type generate_test_ops: bool
663
+
624
664
  :return: :class:`JsonPatch` instance.
625
665
 
626
666
  >>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]}
@@ -632,7 +672,8 @@ class JsonPatch(object):
632
672
  """
633
673
  json_dumper = dumps or cls.json_dumper
634
674
  builder = DiffBuilder(src, dst, json_dumper, pointer_cls=pointer_cls,
635
- optimization=optimization)
675
+ optimization=optimization,
676
+ generate_test_ops=generate_test_ops)
636
677
  builder._compare_values((), None, src, dst)
637
678
  ops = list(builder.execute())
638
679
  return cls(ops, pointer_cls=pointer_cls)
@@ -671,18 +712,18 @@ class JsonPatch(object):
671
712
 
672
713
  def _get_operation(self, operation):
673
714
  if not isinstance(operation, Mapping):
674
- raise InvalidJsonPatch("Operation must be an object")
715
+ raise InvalidJsonPatch("Operation must be an object", op=operation)
675
716
 
676
717
  if 'op' not in operation:
677
- raise InvalidJsonPatch("Operation does not contain 'op' member")
718
+ raise InvalidJsonPatch("Operation does not contain 'op' member", op=operation)
678
719
 
679
720
  op = operation['op']
680
721
 
681
722
  if not isinstance(op, (str, bytes)):
682
- raise InvalidJsonPatch("Operation's op must be a string")
723
+ raise InvalidJsonPatch("Operation's op must be a string", op=operation)
683
724
 
684
725
  if op not in self.operations:
685
- raise InvalidJsonPatch("Unknown operation {0!r}".format(op))
726
+ raise InvalidJsonPatch("Unknown operation {0!r}".format(op), op=operation)
686
727
 
687
728
  cls = self.operations[op]
688
729
  return cls(operation, pointer_cls=self.pointer_cls)
@@ -691,10 +732,14 @@ class JsonPatch(object):
691
732
  class DiffBuilder(object):
692
733
 
693
734
  def __init__(self, src_doc, dst_doc, dumps=json.dumps, pointer_cls=JsonPointer,
694
- optimization=True):
735
+ optimization=True, generate_test_ops=False):
695
736
  self.dumps = dumps
696
737
  self.pointer_cls = pointer_cls
697
738
  self.optimization = optimization
739
+ self.generate_test_ops = generate_test_ops
740
+ # the index of the test operation preceding a remove operation, by
741
+ # the id of the remove operation
742
+ self.remove_tests = {}
698
743
  self.index_storage = [{}, {}]
699
744
  self.__root = root = []
700
745
  self.src_doc = src_doc
@@ -841,6 +886,10 @@ class DiffBuilder(object):
841
886
  source = self._parts(removed)
842
887
  # RFC 6902 does not allow moving a value into its own children
843
888
  if not _is_inside(target, source):
889
+ # the value is moved rather than removed, so it is not tested
890
+ test_index = self.remove_tests.pop(id(index[2]), None)
891
+ if test_index is not None:
892
+ self.remove(test_index)
844
893
  self.remove(index)
845
894
  if source != target:
846
895
  self.insert({'op': 'move', 'from': removed,
@@ -875,14 +924,27 @@ class DiffBuilder(object):
875
924
  self._set_present(added_location, False)
876
925
  return
877
926
 
927
+ test_index = self._insert_test(location, item)
878
928
  self._set_present(location, False)
879
929
  new_index = self.insert({'op': 'remove', 'path': location})
930
+ if test_index is not None:
931
+ self.remove_tests[id(new_index[2])] = test_index
880
932
  self.store_index(item, new_index, _ST_REMOVE)
881
933
 
882
- def _item_replaced(self, path, key, item):
934
+ def _insert_test(self, location, value):
935
+ """ Inserts a test operation that checks the value at location, if
936
+ test operations are generated """
937
+ if self.generate_test_ops:
938
+ # a copy, so that changing src afterwards does not change the test
939
+ return self.insert({'op': 'test', 'path': location,
940
+ 'value': copy.deepcopy(value)})
941
+
942
+ def _item_replaced(self, path, key, item, old_item):
943
+ location = self._location(_path_join(path, key))
944
+ self._insert_test(location, old_item)
883
945
  self.insert({
884
946
  'op': 'replace',
885
- 'path': self._location(_path_join(path, key)),
947
+ 'path': location,
886
948
  'value': item,
887
949
  })
888
950
 
@@ -1017,7 +1079,7 @@ class DiffBuilder(object):
1017
1079
  return
1018
1080
 
1019
1081
  else:
1020
- self._item_replaced(path, key, dst)
1082
+ self._item_replaced(path, key, dst, src)
1021
1083
 
1022
1084
 
1023
1085
  def _positional_cost(old, new):
@@ -18,6 +18,9 @@ jsondiff_parser.add_argument('--indent', type=int, default=None,
18
18
  help='Indent output by n spaces')
19
19
  jsondiff_parser.add_argument('-u', '--preserve-unicode', action='store_true',
20
20
  help='Output Unicode character as-is without using Code Point')
21
+ jsondiff_parser.add_argument('-t', '--test-ops', action='store_true',
22
+ help='Precede each remove and replace operation '
23
+ 'by a test of the value it changes')
21
24
  jsondiff_parser.add_argument('-v', '--version', action='version',
22
25
  version='%(prog)s ' + jsonpatch.__version__)
23
26
 
@@ -34,7 +37,7 @@ def diff_files():
34
37
  args = jsondiff_parser.parse_args()
35
38
  doc1 = json.load(args.FILE1)
36
39
  doc2 = json.load(args.FILE2)
37
- patch = jsonpatch.make_patch(doc1, doc2)
40
+ patch = jsonpatch.make_patch(doc1, doc2, generate_test_ops=args.test_ops)
38
41
  if patch.patch:
39
42
  print(json.dumps(patch.patch, indent=args.indent, ensure_ascii=not(args.preserve_unicode)))
40
43
  sys.exit(1)
@@ -70,8 +73,7 @@ def patch_files():
70
73
  """ Applies a JSON patch on a JSON file """
71
74
  args = jsonpatch_parser.parse_args()
72
75
  doc = json.load(args.ORIGINAL)
73
- patch = json.load(args.PATCH)
74
- result = jsonpatch.apply_patch(doc, patch)
76
+ result = jsonpatch.apply_patch(doc, args.PATCH.read())
75
77
 
76
78
  if args.in_place:
77
79
  # 2026-10-11: Release the input handle before replacing it on Windows.
@@ -299,6 +299,25 @@ class MakePatchProperties(unittest.TestCase):
299
299
  for op in patch))
300
300
  assert_json_equal(patch.apply(src), dst)
301
301
 
302
+ @given(doc_pairs, st.booleans())
303
+ def test_generate_test_ops(self, docs, optimization):
304
+ src, dst = docs
305
+ plain = jsonpatch.JsonPatch.from_diff(
306
+ src, dst, optimization=optimization).patch
307
+ patch = jsonpatch.JsonPatch.from_diff(
308
+ src, dst, optimization=optimization, generate_test_ops=True)
309
+ note(patch.patch)
310
+ # the same operations, each remove and replace preceded by a test
311
+ # of the value it changes, which holds when applied to src
312
+ self.assertEqual([op for op in patch if op['op'] != 'test'], plain)
313
+ expected = []
314
+ for op in plain:
315
+ if op['op'] in ('remove', 'replace'):
316
+ expected.append(('test', op['path']))
317
+ expected.append((op['op'], op['path']))
318
+ self.assertEqual([(op['op'], op['path']) for op in patch], expected)
319
+ assert_json_equal(patch.apply(src), dst)
320
+
302
321
  @given(safe_doc_pairs)
303
322
  # the diff considered e.g. 1 and true equal, #180
304
323
  @example(docs=([0], [False]))
@@ -1322,6 +1322,130 @@ class OptimizationTests(unittest.TestCase):
1322
1322
  ])
1323
1323
 
1324
1324
 
1325
+ class GenerateTestOpsTests(unittest.TestCase):
1326
+ """ make_patch(..., generate_test_ops=True) tests the values it removes
1327
+ or replaces """
1328
+
1329
+ def make_patch(self, src, dst, **kwargs):
1330
+ patch = jsonpatch.make_patch(src, dst, generate_test_ops=True, **kwargs)
1331
+ self.assertEqual(patch.apply(src), dst)
1332
+ return patch.patch
1333
+
1334
+ def test_disabled_by_default(self):
1335
+ patch = jsonpatch.make_patch({'a': 1, 'b': 2}, {'a': 3})
1336
+ self.assertNotIn('test', [op['op'] for op in patch])
1337
+
1338
+ def test_replace(self):
1339
+ self.assertEqual(self.make_patch({'x/y': 1}, {'x/y': 2}), [
1340
+ {'op': 'test', 'path': '/x~1y', 'value': 1},
1341
+ {'op': 'replace', 'path': '/x~1y', 'value': 2},
1342
+ ])
1343
+
1344
+ def test_replace_root(self):
1345
+ self.assertEqual(self.make_patch(1, [1]), [
1346
+ {'op': 'test', 'path': '', 'value': 1},
1347
+ {'op': 'replace', 'path': '', 'value': [1]},
1348
+ ])
1349
+
1350
+ def test_remove(self):
1351
+ src = {'name': 'fred', 'friend': 'barney', 'spouse': 'wilma'}
1352
+ dst = {'name': 'fred', 'spouse': 'wilma'}
1353
+ self.assertEqual(self.make_patch(src, dst), [
1354
+ {'op': 'test', 'path': '/friend', 'value': 'barney'},
1355
+ {'op': 'remove', 'path': '/friend'},
1356
+ ])
1357
+
1358
+ def test_replaced_array_item(self):
1359
+ # a remove and an add of the same array item are merged to a replace
1360
+ self.assertEqual(self.make_patch({'foo': [1, 2, 3]}, {'foo': [3, 2, 3]}), [
1361
+ {'op': 'test', 'path': '/foo/0', 'value': 1},
1362
+ {'op': 'replace', 'path': '/foo/0', 'value': 3},
1363
+ ])
1364
+
1365
+ def test_array_indices_of_tests(self):
1366
+ # each test is at the index the item has when it is removed
1367
+ self.assertEqual(self.make_patch([1, 2, 3, 4], [2, 4]), [
1368
+ {'op': 'test', 'path': '/0', 'value': 1},
1369
+ {'op': 'remove', 'path': '/0'},
1370
+ {'op': 'test', 'path': '/1', 'value': 3},
1371
+ {'op': 'remove', 'path': '/1'},
1372
+ ])
1373
+
1374
+ def test_moves_are_not_tested(self):
1375
+ # the removed value is moved instead, so it is not tested
1376
+ self.assertEqual(self.make_patch({'a': {'x': 1}}, {'b': {'x': 1}}),
1377
+ [{'op': 'move', 'from': '/a', 'path': '/b'}])
1378
+ self.assertEqual(self.make_patch([4, 1, 2, 3], [1, 2, 3, 4]),
1379
+ [{'op': 'move', 'from': '/0', 'path': '/3'}])
1380
+
1381
+ def test_without_move_optimization(self):
1382
+ patch = jsonpatch.JsonPatch.from_diff(
1383
+ {'a': {'x': 1}}, {'b': {'x': 1}}, optimization=False,
1384
+ generate_test_ops=True)
1385
+ self.assertEqual(patch.patch, [
1386
+ {'op': 'test', 'path': '/a', 'value': {'x': 1}},
1387
+ {'op': 'remove', 'path': '/a'},
1388
+ {'op': 'add', 'path': '/b', 'value': {'x': 1}},
1389
+ ])
1390
+
1391
+ def test_same_operations_besides_tests(self):
1392
+ documents = [
1393
+ ({'foo': 'bar', 'numbers': [1, 3, 4, 8]},
1394
+ {'baz': 'qux', 'numbers': [1, 4, 7]}),
1395
+ ([8, 7, 2, 1, 0, 9, 4, 3, 5, 6], [7, 2, 1, 0, 9, 4, 3, 6, 5, 8]),
1396
+ ({'school': {'names': ['Kevin', 'Carl']}},
1397
+ {'school': {'names': ['Carl', 'Kate', 'Kevin', 'Jake']}}),
1398
+ ({'A': {'D': 1.0}, 'B': {'E': 'a'}},
1399
+ {'A': {'C': 'a'}, 'B': {'C': True}}),
1400
+ ([{'a': 1, 'b': 2, 'd': 5}], [{'d': 6}]),
1401
+ ]
1402
+ for src, dst in documents:
1403
+ with self.subTest(src=src, dst=dst):
1404
+ plain = jsonpatch.make_patch(src, dst).patch
1405
+ tested = self.make_patch(src, dst)
1406
+ self.assertEqual([op for op in tested if op['op'] != 'test'],
1407
+ plain)
1408
+ for i, op in enumerate(tested):
1409
+ if op['op'] == 'test':
1410
+ self.assertIn(tested[i + 1]['op'],
1411
+ ('remove', 'replace'))
1412
+ self.assertEqual(tested[i + 1]['path'], op['path'])
1413
+ self.assertEqual(
1414
+ sum(op['op'] == 'test' for op in tested),
1415
+ sum(op['op'] in ('remove', 'replace') for op in plain))
1416
+
1417
+ def test_fails_on_changed_values(self):
1418
+ patch = jsonpatch.make_patch({'a': 1, 'b': [1, 2]}, {'b': [1]},
1419
+ generate_test_ops=True)
1420
+ for doc in ({'a': 2, 'b': [1, 2]}, {'a': 1, 'b': [1, 3]}):
1421
+ with self.subTest(doc=doc):
1422
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed):
1423
+ patch.apply(doc)
1424
+
1425
+ def test_tested_values_are_copies(self):
1426
+ # changing src after diffing does not change what is tested
1427
+ src = {'a': {'nested': [1]}, 'b': [1]}
1428
+ original = copy.deepcopy(src)
1429
+ patch = jsonpatch.make_patch(src, {'b': 2}, generate_test_ops=True)
1430
+ src['a']['nested'].append(2)
1431
+ src['b'].append(2)
1432
+ self.assertEqual(patch.patch, [
1433
+ {'op': 'test', 'path': '/a', 'value': {'nested': [1]}},
1434
+ {'op': 'remove', 'path': '/a'},
1435
+ {'op': 'test', 'path': '/b', 'value': [1]},
1436
+ {'op': 'replace', 'path': '/b', 'value': 2},
1437
+ ])
1438
+ self.assertEqual(patch.apply(original), {'b': 2})
1439
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed):
1440
+ patch.apply(src)
1441
+
1442
+ def test_from_diff(self):
1443
+ patch = jsonpatch.JsonPatch.from_diff({'a': 1}, {},
1444
+ generate_test_ops=True)
1445
+ self.assertEqual(patch.patch, [{'op': 'test', 'path': '/a', 'value': 1},
1446
+ {'op': 'remove', 'path': '/a'}])
1447
+
1448
+
1325
1449
  class ArrayItemsTests(unittest.TestCase):
1326
1450
  """ The labels that order the items of an array in make_patch run out
1327
1451
  between two items, and have to be spread out again. make_patch gets there
@@ -1961,6 +2085,67 @@ class CustomJsonPointerTests(unittest.TestCase):
1961
2085
  self.assertEqual(res, {'foo': {'bar': {'baz': 'qux'}}})
1962
2086
 
1963
2087
 
2088
+ class ExceptionOperationTests(unittest.TestCase):
2089
+ """ The failing operation is attached to raised exceptions """
2090
+
2091
+ def test_conflict_has_operation(self):
2092
+ patch = jsonpatch.JsonPatch([{'op': 'remove', 'path': '/missing'}])
2093
+ with self.assertRaises(jsonpatch.JsonPatchConflict) as cm:
2094
+ patch.apply({'foo': 1})
2095
+ self.assertIsInstance(cm.exception.op, jsonpatch.RemoveOperation)
2096
+ self.assertEqual(cm.exception.op.location, '/missing')
2097
+ self.assertEqual(str(cm.exception),
2098
+ "can't remove a non-existent object 'missing'")
2099
+
2100
+ def test_test_failed_has_operation(self):
2101
+ patch = jsonpatch.JsonPatch([{'op': 'test', 'path': '/foo', 'value': 2}])
2102
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed) as cm:
2103
+ patch.apply({'foo': 1})
2104
+ self.assertIsInstance(cm.exception.op, jsonpatch.TestOperation)
2105
+
2106
+ def test_invalid_patch_has_raw_operation(self):
2107
+ operation = {'op': 'frobnicate', 'path': '/foo'}
2108
+ with self.assertRaises(jsonpatch.InvalidJsonPatch) as cm:
2109
+ jsonpatch.apply_patch({}, [operation])
2110
+ self.assertIs(cm.exception.op, operation)
2111
+
2112
+ def test_move_failed_add_reports_move_operation(self):
2113
+ patch = jsonpatch.JsonPatch(
2114
+ [{'op': 'move', 'from': '/foo', 'path': '/bar/5'}])
2115
+ with self.assertRaises(jsonpatch.JsonPatchConflict) as cm:
2116
+ patch.apply({'foo': 1, 'bar': []})
2117
+ self.assertIsInstance(cm.exception.op, jsonpatch.MoveOperation)
2118
+ self.assertIs(cm.exception.op.operation, patch.patch[0])
2119
+
2120
+ def test_move_failed_remove_reports_move_operation(self):
2121
+ # the value can be read from the immutable tuple, but the internal
2122
+ # remove operation fails
2123
+ patch = jsonpatch.JsonPatch(
2124
+ [{'op': 'move', 'from': '/foo/0', 'path': '/bar'}])
2125
+ with self.assertRaises(jsonpatch.JsonPatchConflict) as cm:
2126
+ patch.apply({'foo': (1, 2)})
2127
+ self.assertIsInstance(cm.exception.op, jsonpatch.MoveOperation)
2128
+ self.assertIs(cm.exception.op.operation, patch.patch[0])
2129
+
2130
+ def test_copy_failed_add_reports_copy_operation(self):
2131
+ patch = jsonpatch.JsonPatch(
2132
+ [{'op': 'copy', 'from': '/foo', 'path': '/bar/5'}])
2133
+ with self.assertRaises(jsonpatch.JsonPatchConflict) as cm:
2134
+ patch.apply({'foo': 1, 'bar': []})
2135
+ self.assertIsInstance(cm.exception.op, jsonpatch.CopyOperation)
2136
+ self.assertIs(cm.exception.op.operation, patch.patch[0])
2137
+
2138
+ def test_string_entry_has_raw_operation(self):
2139
+ with self.assertRaises(jsonpatch.InvalidJsonPatch) as cm:
2140
+ jsonpatch.JsonPatch(['invalid operation'])
2141
+ self.assertEqual(cm.exception.op, 'invalid operation')
2142
+
2143
+ def test_op_defaults_to_none(self):
2144
+ ex = jsonpatch.JsonPatchException('message')
2145
+ self.assertIsNone(ex.op)
2146
+ self.assertEqual(str(ex), 'message')
2147
+
2148
+
1964
2149
  class CustomOperationTests(unittest.TestCase):
1965
2150
 
1966
2151
  def test_custom_operation(self):
@@ -2048,6 +2233,20 @@ class CommandLineTests(ScriptRunner, unittest.TestCase):
2048
2233
  patch = jsonpatch.make_patch({'a': 1}, {'b': 1}).patch
2049
2234
  self.assertEqual(res.stdout, json.dumps(patch, indent=2) + '\n')
2050
2235
 
2236
+ def test_jsondiff_test_ops(self):
2237
+ src = self.write('a.json', {'a': 1, 'b': 2})
2238
+ dst = self.write('b.json', {'a': 3})
2239
+ for option in ('-t', '--test-ops'):
2240
+ with self.subTest(option=option):
2241
+ res = self.run_script('jsondiff', option, src, dst)
2242
+ self.assertEqual(res.returncode, 1, res.stderr)
2243
+ self.assertEqual(json.loads(res.stdout), [
2244
+ {'op': 'test', 'path': '/b', 'value': 2},
2245
+ {'op': 'remove', 'path': '/b'},
2246
+ {'op': 'test', 'path': '/a', 'value': 1},
2247
+ {'op': 'replace', 'path': '/a', 'value': 3},
2248
+ ])
2249
+
2051
2250
  def test_jsondiff_preserve_unicode(self):
2052
2251
  src = self.write('a.json', {'a': 'x'})
2053
2252
  dst = self.write('b.json', {'a': 'ä'})
@@ -2082,6 +2281,27 @@ class CommandLineTests(ScriptRunner, unittest.TestCase):
2082
2281
  self.assertEqual(res.returncode, 0, res.stderr)
2083
2282
  self.assertEqual(json.loads(res.stdout), {'a': 2})
2084
2283
 
2284
+ def test_jsonpatch_rejects_invalid_patch_text_before_in_place_write(self):
2285
+ remove = '[{"op":"remove","path":"/keep"}]'
2286
+ for patch in ('[{"op":"test","op":"remove","path":"/keep","value":13}]',
2287
+ json.dumps(remove)):
2288
+ for use_stdin in (False, True):
2289
+ with self.subTest(patch=patch, stdin=use_stdin):
2290
+ doc = self.write('doc.json', {'keep': 7})
2291
+ with open(doc, 'rb') as f:
2292
+ original = f.read()
2293
+ if use_stdin:
2294
+ res = self.run_script('jsonpatch', '-i', doc, stdin=patch)
2295
+ else:
2296
+ path = os.path.join(self.dir, 'patch.json')
2297
+ with open(path, 'w', encoding='utf-8') as f:
2298
+ f.write(patch)
2299
+ res = self.run_script('jsonpatch', '-i', doc, path)
2300
+ self.assertNotEqual(res.returncode, 0)
2301
+ self.assertIn('InvalidJsonPatch', res.stderr)
2302
+ with open(doc, 'rb') as f:
2303
+ self.assertEqual(f.read(), original)
2304
+
2085
2305
  def test_jsonpatch_utf8_files_without_utf8_mode(self):
2086
2306
  doc = self.write('doc.json', {'a': 'ä'}, ensure_ascii=False)
2087
2307
  patch = self.write('patch.json', [
@@ -2420,6 +2640,7 @@ if __name__ == '__main__':
2420
2640
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ConflictTests))
2421
2641
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(StringIndexingTests))
2422
2642
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(OptimizationTests))
2643
+ suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(GenerateTestOpsTests))
2423
2644
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ArrayItemsTests))
2424
2645
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(JsonPointerTests))
2425
2646
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(JsonPatchCreationTest))
@@ -2427,6 +2648,7 @@ if __name__ == '__main__':
2427
2648
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(OperationLocationTests))
2428
2649
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(CustomJsonPointerTests))
2429
2650
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(CustomOperationTests))
2651
+ suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ExceptionOperationTests))
2430
2652
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(CommandLineTests))
2431
2653
  suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(DocumentationTests))
2432
2654
  return suite
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