Skip to content

Commit bafa36c

Browse files
committed
Report move/copy and string-entry failures against the original operation
- MoveOperation and CopyOperation delegate to internal, synthetic RemoveOperation / AddOperation instances. Exceptions raised by those now have `op` set to the enclosing move/copy operation, so callers can identify the operation that is actually part of the patch. - The "sequence of strings" check in JsonPatch.__init__ now attaches the offending entry as `op`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PewzpkvyJUvFMnnjKxmyNd
1 parent 5eccc03 commit bafa36c

2 files changed

Lines changed: 60 additions & 15 deletions

File tree

‎jsonpatch.py‎

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -385,16 +385,23 @@ def apply(self, obj):
385385
if self.pointer == from_ptr:
386386
return obj
387387

388-
obj = RemoveOperation({
389-
'op': 'remove',
390-
'path': self.operation['from']
391-
}, pointer_cls=self.pointer_cls).apply(obj)
392-
393-
# the value has been detached from its old location, so no copy needed
394-
obj = AddOperation({
395-
'op': 'add',
396-
'path': self.location,
397-
}, pointer_cls=self.pointer_cls)._add(obj, value)
388+
try:
389+
obj = RemoveOperation({
390+
'op': 'remove',
391+
'path': self.operation['from']
392+
}, pointer_cls=self.pointer_cls).apply(obj)
393+
394+
# the value has been detached from its old location, so no copy
395+
# needed
396+
obj = AddOperation({
397+
'op': 'add',
398+
'path': self.location,
399+
}, pointer_cls=self.pointer_cls)._add(obj, value)
400+
except JsonPatchException as ex:
401+
# report the failure against this operation rather than the
402+
# internal, synthetic remove / add operations
403+
ex.op = self
404+
raise
398405

399406
return obj
400407

@@ -464,10 +471,16 @@ def apply(self, obj):
464471
raise JsonPatchConflict(str(ex), op=self)
465472

466473
# value is already a deep copy, no need to copy it again
467-
obj = AddOperation({
468-
'op': 'add',
469-
'path': self.location,
470-
}, pointer_cls=self.pointer_cls)._add(obj, value)
474+
try:
475+
obj = AddOperation({
476+
'op': 'add',
477+
'path': self.location,
478+
}, pointer_cls=self.pointer_cls)._add(obj, value)
479+
except JsonPatchException as ex:
480+
# report the failure against this operation rather than the
481+
# internal, synthetic add operation
482+
ex.op = self
483+
raise
471484

472485
return obj
473486

@@ -560,7 +573,8 @@ def __init__(self, patch, pointer_cls=JsonPointer):
560573
# of mappings, this won't raise.
561574
if isinstance(op, (str, bytes)):
562575
raise InvalidJsonPatch("Document is expected to be sequence of "
563-
"operations, got a sequence of strings.")
576+
"operations, got a sequence of strings.",
577+
op=op)
564578

565579
self._get_operation(op)
566580

‎tests.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,37 @@ def test_invalid_patch_has_raw_operation(self):
19851985
jsonpatch.apply_patch({}, [operation])
19861986
self.assertIs(cm.exception.op, operation)
19871987

1988+
def test_move_failed_add_reports_move_operation(self):
1989+
patch = jsonpatch.JsonPatch(
1990+
[{'op': 'move', 'from': '/foo', 'path': '/bar/5'}])
1991+
with self.assertRaises(jsonpatch.JsonPatchConflict) as cm:
1992+
patch.apply({'foo': 1, 'bar': []})
1993+
self.assertIsInstance(cm.exception.op, jsonpatch.MoveOperation)
1994+
self.assertIs(cm.exception.op.operation, patch.patch[0])
1995+
1996+
def test_move_failed_remove_reports_move_operation(self):
1997+
# the value can be read from the immutable tuple, but the internal
1998+
# remove operation fails
1999+
patch = jsonpatch.JsonPatch(
2000+
[{'op': 'move', 'from': '/foo/0', 'path': '/bar'}])
2001+
with self.assertRaises(jsonpatch.JsonPatchConflict) as cm:
2002+
patch.apply({'foo': (1, 2)})
2003+
self.assertIsInstance(cm.exception.op, jsonpatch.MoveOperation)
2004+
self.assertIs(cm.exception.op.operation, patch.patch[0])
2005+
2006+
def test_copy_failed_add_reports_copy_operation(self):
2007+
patch = jsonpatch.JsonPatch(
2008+
[{'op': 'copy', 'from': '/foo', 'path': '/bar/5'}])
2009+
with self.assertRaises(jsonpatch.JsonPatchConflict) as cm:
2010+
patch.apply({'foo': 1, 'bar': []})
2011+
self.assertIsInstance(cm.exception.op, jsonpatch.CopyOperation)
2012+
self.assertIs(cm.exception.op.operation, patch.patch[0])
2013+
2014+
def test_string_entry_has_raw_operation(self):
2015+
with self.assertRaises(jsonpatch.InvalidJsonPatch) as cm:
2016+
jsonpatch.JsonPatch(['invalid operation'])
2017+
self.assertEqual(cm.exception.op, 'invalid operation')
2018+
19882019
def test_op_defaults_to_none(self):
19892020
ex = jsonpatch.JsonPatchException('message')
19902021
self.assertIsNone(ex.op)

0 commit comments

Comments
 (0)