Skip to content

Commit d725141

Browse files
committed
Merge pull request #21 from johanfforsberg/array-move
Moving array item into other array item
2 parents 48f9adb + 7e459af commit d725141

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

jsonpatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def apply(self, obj):
466466
subobj, part = from_ptr.to_last(obj)
467467
value = subobj[part]
468468

469-
if self.pointer.contains(from_ptr):
469+
if isinstance(subobj, dict) and self.pointer.contains(from_ptr):
470470
raise JsonPatchException('Cannot move values into its own children')
471471

472472
obj = RemoveOperation({

tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ def test_move_array_item(self):
8888
res = jsonpatch.apply_patch(obj, [{'op': 'move', 'from': '/foo/1', 'path': '/foo/3'}])
8989
self.assertEqual(res, {'foo': ['all', 'cows', 'eat', 'grass']})
9090

91+
def test_move_array_item_into_other_item(self):
92+
obj = [{"foo": []}, {"bar": []}]
93+
patch = [{"op": "move", "from": "/0", "path": "/0/bar/0"}]
94+
res = jsonpatch.apply_patch(obj, patch)
95+
self.assertEqual(res, [{'bar': [{"foo": []}]}])
96+
9197
def test_copy_object_key(self):
9298
obj = {'foo': {'bar': 'baz', 'waldo': 'fred'},
9399
'qux': {'corge': 'grault'}}

0 commit comments

Comments
 (0)