Skip to content

Commit 1ebce1e

Browse files
authored
Merge pull request #272 from stefankoegl/claude/pointer-object-paths
Serialize and compare patches whose paths are pointer objects
2 parents 5f5cbda + 42c8410 commit 1ebce1e

2 files changed

Lines changed: 113 additions & 4 deletions

File tree

‎jsonpatch.py‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,23 @@ def _reorder(mapping, keys):
437437
mapping[key] = mapping.pop(key)
438438

439439

440+
def _plain_operation(operation, pointer_cls):
441+
"""Returns operation as a dict whose 'path' and 'from' are strings.
442+
443+
Both can be given as pointer objects instead. These are replaced by
444+
their paths, so that the operation is JSON-serializable, and can be
445+
compared to one that was given the paths. This is done in a copy; an
446+
operation that is a dict already and has no pointer objects is returned
447+
as it is.
448+
"""
449+
paths = {member: operation[member].path for member in ('path', 'from')
450+
if member in operation and
451+
isinstance(operation[member], pointer_cls)}
452+
if paths:
453+
return dict(operation, **paths)
454+
return operation if isinstance(operation, dict) else dict(operation)
455+
456+
440457
class PatchOperation:
441458
"""A single operation inside a JSON Patch."""
442459

@@ -491,13 +508,17 @@ def _apply(self, obj, journal):
491508
def __hash__(self):
492509
# Other members can hold arrays or objects, which are unhashable.
493510
# Operations that compare equal still have equal 'op' and 'path'.
494-
return hash((self.operation.get('op'), self.operation['path']))
511+
operation = _plain_operation(self.operation, self.pointer_cls)
512+
return hash((operation.get('op'), operation['path']))
495513

496514
def __eq__(self, other):
497515
if not isinstance(other, PatchOperation):
498516
return False
499517
# e.g. tests for 1 and for true behave differently
500-
return _json_equal(self.operation, other.operation)
518+
# and paths are equal to pointer objects for them
519+
return _json_equal(
520+
_plain_operation(self.operation, self.pointer_cls),
521+
_plain_operation(other.operation, other.pointer_cls))
501522

502523
@property
503524
def path(self):
@@ -962,8 +983,7 @@ def __init__(self, patch, pointer_cls=JsonPointer):
962983
self._get_operation(op)
963984
# Keep validation errors tied to the original input, but expose
964985
# concrete, JSON-serializable operation dictionaries on success.
965-
if not isinstance(op, dict):
966-
self.patch[index] = dict(op)
986+
self.patch[index] = _plain_operation(op, self.pointer_cls)
967987

968988
def __str__(self):
969989
"""str(self) -> self.to_string()"""

‎tests.py‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,95 @@ def test_copy_from_pointer(self):
19871987
])
19881988
self.assertEqual(patch.apply({'foo': 1}), {'foo': 1, 'bar': 1})
19891989

1990+
def pointer_and_string_operations(self, pointer_cls=jsonpointer.JsonPointer,
1991+
paths=('/foo', '/bar/0', '')):
1992+
foo, bar, root = paths
1993+
with_strings = [
1994+
{'op': 'add', 'path': foo, 'value': [1]},
1995+
{'op': 'move', 'from': foo, 'path': bar},
1996+
{'op': 'copy', 'from': root, 'path': foo},
1997+
{'op': 'copy', 'from': foo, 'path': bar},
1998+
]
1999+
with_pointers = [
2000+
{'op': 'add', 'path': pointer_cls(foo), 'value': [1]},
2001+
{'op': 'move', 'from': pointer_cls(foo), 'path': pointer_cls(bar)},
2002+
{'op': 'copy', 'from': pointer_cls(root), 'path': foo},
2003+
{'op': 'copy', 'from': foo, 'path': pointer_cls(bar)},
2004+
]
2005+
return with_pointers, with_strings
2006+
2007+
def test_patch_with_pointers_is_serializable(self):
2008+
with_pointers, with_strings = self.pointer_and_string_operations()
2009+
patch = jsonpatch.JsonPatch(with_pointers)
2010+
self.assertEqual(patch.patch, with_strings)
2011+
self.assertEqual(list(patch), with_strings)
2012+
self.assertEqual(json.loads(patch.to_string()), with_strings)
2013+
self.assertEqual(json.loads(str(patch)), with_strings)
2014+
self.assertEqual(jsonpatch.JsonPatch.from_string(patch.to_string()), patch)
2015+
2016+
def test_patch_with_pointers_equals_patch_with_strings(self):
2017+
with_pointers, with_strings = self.pointer_and_string_operations()
2018+
patch = jsonpatch.JsonPatch(with_pointers)
2019+
other = jsonpatch.JsonPatch(with_strings)
2020+
self.assertEqual(patch, other)
2021+
self.assertEqual(hash(patch), hash(other))
2022+
self.assertEqual(len({patch, other}), 1)
2023+
doc = {'bar': []}
2024+
self.assertEqual(patch.apply(doc), other.apply(doc))
2025+
2026+
def test_patch_with_pointers_does_not_change_the_operations(self):
2027+
with_pointers, _ = self.pointer_and_string_operations()
2028+
pointers = [(op, member, op[member]) for op in with_pointers
2029+
for member in ('path', 'from')
2030+
if isinstance(op.get(member), jsonpointer.JsonPointer)]
2031+
jsonpatch.JsonPatch(with_pointers).apply({'bar': []})
2032+
self.assertEqual(len(pointers), 5)
2033+
for op, member, pointer in pointers:
2034+
self.assertIs(op[member], pointer)
2035+
2036+
def test_patch_with_pointers_as_read_only_mappings(self):
2037+
with_pointers, with_strings = self.pointer_and_string_operations()
2038+
patch = jsonpatch.JsonPatch(map(MappingProxyType, with_pointers))
2039+
self.assertEqual(patch.patch, with_strings)
2040+
self.assertTrue(all(type(op) is dict for op in patch))
2041+
2042+
def test_patch_with_pointers_of_custom_class(self):
2043+
with_pointers, with_strings = self.pointer_and_string_operations(
2044+
DotJsonPointer, ('foo', 'bar.0', ''))
2045+
patch = jsonpatch.JsonPatch(with_pointers, pointer_cls=DotJsonPointer)
2046+
other = jsonpatch.JsonPatch(with_strings, pointer_cls=DotJsonPointer)
2047+
self.assertEqual(patch.patch, with_strings)
2048+
self.assertEqual(patch, other)
2049+
self.assertEqual(patch.apply({'bar': []}), other.apply({'bar': []}))
2050+
2051+
def test_operation_with_pointers_equals_operation_with_strings(self):
2052+
with_pointers, with_strings = self.pointer_and_string_operations()
2053+
for pointers, strings in zip(with_pointers, with_strings):
2054+
with self.subTest(operation=strings):
2055+
cls = jsonpatch.JsonPatch.operations[strings['op']]
2056+
operation, other = cls(pointers), cls(strings)
2057+
self.assertEqual(operation, other)
2058+
self.assertEqual(other, operation)
2059+
self.assertEqual(hash(operation), hash(other))
2060+
self.assertEqual(operation.location, other.location)
2061+
# setters change the operation it was created from, so it
2062+
# is not replaced by a copy without pointers
2063+
self.assertIs(operation.operation, pointers)
2064+
self.assertNotEqual(
2065+
jsonpatch.RemoveOperation({'op': 'remove', 'path': jsonpointer.JsonPointer('/a')}),
2066+
jsonpatch.RemoveOperation({'op': 'remove', 'path': '/b'}))
2067+
2068+
def test_operation_setters_with_pointers(self):
2069+
pointers = {'op': 'move', 'from': jsonpointer.JsonPointer('/a/b'),
2070+
'path': jsonpointer.JsonPointer('/c/d')}
2071+
operation = jsonpatch.MoveOperation(pointers)
2072+
operation.key = 'e'
2073+
operation.from_key = 'f'
2074+
self.assertEqual(pointers,
2075+
{'op': 'move', 'from': '/a/f', 'path': '/c/e'})
2076+
self.assertEqual(operation.apply({'a': {'f': 1}, 'c': {}}),
2077+
{'a': {}, 'c': {'e': 1}})
2078+
19902079

19912080
class JsonPatchCreationTest(unittest.TestCase):
19922081

0 commit comments

Comments
 (0)