diff --git a/jsonpatch.py b/jsonpatch.py index 3123eca..a0c2803 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -384,7 +384,7 @@ def apply(self, obj): @property def from_path(self): - from_ptr = self.pointer_cls(self.operation['from']) + from_ptr = self._from_pointer() return '/'.join(from_ptr.parts[:-1]) @property @@ -396,14 +396,14 @@ def from_key(self, value): self.set_from_part(-1, value) def get_from_part(self, index): - from_ptr = self.pointer_cls(self.operation['from']) + from_ptr = self._from_pointer() try: return int(from_ptr.parts[index]) except ValueError: return from_ptr.parts[index] def set_from_part(self, index, value): - from_ptr = self.pointer_cls(self.operation['from']) + from_ptr = self._from_pointer() from_ptr.parts[index] = str(value) self.operation['from'] = from_ptr.path diff --git a/tests.py b/tests.py index 124b086..f6a850b 100755 --- a/tests.py +++ b/tests.py @@ -1676,6 +1676,34 @@ def test_set_from_key(self): self.assertEqual(operation, {'op': 'move', 'from': '/c~1d/0', 'path': '/b'}) self.assertEqual(op.apply({'c/d': ['x']}), {'c/d': [], 'b': 'x'}) + def test_from_path_with_pointer_object(self): + for pointer_cls in (jsonpointer.JsonPointer, CustomJsonPointer): + with self.subTest(pointer_cls=pointer_cls): + op = jsonpatch.MoveOperation( + {'op': 'move', 'from': pointer_cls('/a/1/b'), 'path': '/c'}, + pointer_cls=pointer_cls) + self.assertEqual(op.from_path, 'a/1') + + def test_from_parts_with_pointer_object(self): + for pointer_cls in (jsonpointer.JsonPointer, CustomJsonPointer): + with self.subTest(pointer_cls=pointer_cls): + op = jsonpatch.MoveOperation( + {'op': 'move', 'from': pointer_cls('/a/1/b'), 'path': '/c'}, + pointer_cls=pointer_cls) + self.assertEqual(op.from_key, 'b') + self.assertEqual(op.get_from_part(1), 1) + + def test_set_from_key_with_pointer_object(self): + for pointer_cls in (jsonpointer.JsonPointer, CustomJsonPointer): + with self.subTest(pointer_cls=pointer_cls): + operation = {'op': 'move', 'from': pointer_cls('/a/1'), + 'path': '/b'} + op = jsonpatch.MoveOperation(operation, pointer_cls=pointer_cls) + op.from_key = 0 + self.assertEqual(operation['from'], '/a/0') + self.assertEqual(op.apply({'a': ['x', 'y']}), + {'a': ['y'], 'b': 'x'}) + class CustomJsonPointer(jsonpointer.JsonPointer): pass