summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2014-01-12 02:12:46 -0800
committerStefan Kögl <stefan@skoegl.net>2014-01-12 02:12:46 -0800
commitd725141a0d5bbd0f9446cc9e3820ccd2ff78cbcd (patch)
tree19f9d21932864b54df0f8cf404150de3c339efb1
parent48f9adb8991deb62ba4f327d5e96766c4d0d55e7 (diff)
parent7e459af293954675a5ac9852008666bfd95187ea (diff)
downloadpython-json-patch-d725141a0d5bbd0f9446cc9e3820ccd2ff78cbcd.tar.gz
Merge pull request #21 from johanfforsberg/array-move
Moving array item into other array item
-rw-r--r--jsonpatch.py2
-rwxr-xr-xtests.py6
2 files changed, 7 insertions, 1 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index c614666..a46c9b0 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -466,7 +466,7 @@ class MoveOperation(PatchOperation):
subobj, part = from_ptr.to_last(obj)
value = subobj[part]
- if self.pointer.contains(from_ptr):
+ if isinstance(subobj, dict) and self.pointer.contains(from_ptr):
raise JsonPatchException('Cannot move values into its own children')
obj = RemoveOperation({
diff --git a/tests.py b/tests.py
index 3fbffbb..c071ce2 100755
--- a/tests.py
+++ b/tests.py
@@ -88,6 +88,12 @@ class ApplyPatchTestCase(unittest.TestCase):
res = jsonpatch.apply_patch(obj, [{'op': 'move', 'from': '/foo/1', 'path': '/foo/3'}])
self.assertEqual(res, {'foo': ['all', 'cows', 'eat', 'grass']})
+ def test_move_array_item_into_other_item(self):
+ obj = [{"foo": []}, {"bar": []}]
+ patch = [{"op": "move", "from": "/0", "path": "/0/bar/0"}]
+ res = jsonpatch.apply_patch(obj, patch)
+ self.assertEqual(res, [{'bar': [{"foo": []}]}])
+
def test_copy_object_key(self):
obj = {'foo': {'bar': 'baz', 'waldo': 'fred'},
'qux': {'corge': 'grault'}}