Skip to content

Commit 0c96a53

Browse files
authored
Merge pull request #75 from stefankoegl/jsonptr
Expect path/from attributes also as JsonPoiner instances (#60)
2 parents 3c621da + aae6082 commit 0c96a53

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

jsonpatch.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,14 @@ class PatchOperation(object):
333333
"""A single operation inside a JSON Patch."""
334334

335335
def __init__(self, operation):
336-
self.location = operation['path']
337-
self.pointer = JsonPointer(self.location)
336+
337+
if isinstance(operation['path'], JsonPointer):
338+
self.location = operation['path'].path
339+
self.pointer = operation['path']
340+
else:
341+
self.location = operation['path']
342+
self.pointer = JsonPointer(self.location)
343+
338344
self.operation = operation
339345

340346
def apply(self, obj):
@@ -491,7 +497,10 @@ class MoveOperation(PatchOperation):
491497

492498
def apply(self, obj):
493499
try:
494-
from_ptr = JsonPointer(self.operation['from'])
500+
if isinstance(self.operation['from'], JsonPointer):
501+
from_ptr = self.operation['from']
502+
else:
503+
from_ptr = JsonPointer(self.operation['from'])
495504
except KeyError as ex:
496505
raise InvalidJsonPatch(
497506
"The operation does not contain a 'from' member")

tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,27 @@ def test_replace_missing(self):
608608
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)
609609

610610

611+
class JsonPointerTests(unittest.TestCase):
612+
613+
def test_create_with_pointer(self):
614+
615+
patch = jsonpatch.JsonPatch([
616+
{'op': 'add', 'path': jsonpointer.JsonPointer('/foo'), 'value': 'bar'},
617+
{'op': 'add', 'path': jsonpointer.JsonPointer('/baz'), 'value': [1, 2, 3]},
618+
{'op': 'remove', 'path': jsonpointer.JsonPointer('/baz/1')},
619+
{'op': 'test', 'path': jsonpointer.JsonPointer('/baz'), 'value': [1, 3]},
620+
{'op': 'replace', 'path': jsonpointer.JsonPointer('/baz/0'), 'value': 42},
621+
{'op': 'remove', 'path': jsonpointer.JsonPointer('/baz/1')},
622+
{'op': 'move', 'from': jsonpointer.JsonPointer('/foo'), 'path': jsonpointer.JsonPointer('/bar')},
623+
624+
])
625+
doc = {}
626+
result = patch.apply(doc)
627+
expected = {'bar': 'bar', 'baz': [42]}
628+
self.assertEqual(result, expected)
629+
630+
631+
611632
if __name__ == '__main__':
612633
modules = ['jsonpatch']
613634

@@ -622,6 +643,7 @@ def get_suite():
622643
suite.addTest(unittest.makeSuite(InvalidInputTests))
623644
suite.addTest(unittest.makeSuite(ConflictTests))
624645
suite.addTest(unittest.makeSuite(OptimizationTests))
646+
suite.addTest(unittest.makeSuite(JsonPointerTests))
625647
return suite
626648

627649

0 commit comments

Comments
 (0)