summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2012-09-14 15:22:42 +0200
committerStefan Kögl <stefan@skoegl.net>2012-09-14 15:25:37 +0200
commit46334177e5e84b9772bb527ccea3128f6ad29dcd (patch)
tree12df258f818c384bf24aa29153cec94a7269bfa4
parent3829275ade689224dfe48ec503b9204e40a6723c (diff)
downloadpython-json-patch-46334177e5e84b9772bb527ccea3128f6ad29dcd.tar.gz
"copy" operation should copy by value, not by reference, fixes #8
-rw-r--r--jsonpatch.py2
-rwxr-xr-xtests.py12
2 files changed, 13 insertions, 1 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index 5dac4a1..d102e7e 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -422,5 +422,5 @@ class CopyOperation(PatchOperation):
def apply(self, obj):
subobj, part = self.locate(obj, self.location)
- value = subobj[part]
+ value = copy.deepcopy(subobj[part])
AddOperation(self.operation['to'], {'value': value}).apply(obj)
diff --git a/tests.py b/tests.py
index 4cf0ff2..fd07f9d 100755
--- a/tests.py
+++ b/tests.py
@@ -86,6 +86,18 @@ class ApplyPatchTestCase(unittest.TestCase):
self.assertEqual(res, {'foo': ['all', 'grass', 'cows', 'grass', 'eat']})
+ def test_copy_mutable(self):
+ """ test if mutable objects (dicts and lists) are copied by value """
+ obj = {'foo': [{'bar': 42}, {'baz': 3.14}], 'boo': []}
+ # copy object somewhere
+ res = jsonpatch.apply_patch(obj, [{'copy': '/foo/0', 'to': '/boo/0' }])
+ self.assertEqual(res, {'foo': [{'bar': 42}, {'baz': 3.14}], 'boo': [{'bar': 42}]})
+ # modify original object
+ res = jsonpatch.apply_patch(res, [{'add': '/foo/0/zoo', 'value': 255}])
+ # check if that didn't modify the copied object
+ self.assertEqual(res['boo'], [{'bar': 42}])
+
+
def test_test_success(self):
obj = {'baz': 'qux', 'foo': ['a', 2, 'c']}
jsonpatch.apply_patch(obj, [{'test': '/baz', 'value': 'qux'},