summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2013-04-03 15:37:55 +0200
committerStefan Kögl <stefan@skoegl.net>2013-04-03 15:37:55 +0200
commitf3e067281fbee0829b7bb2656bbef91dffc9146a (patch)
tree23fb5d9d413665416fe420f3c3855d5d4861b54d
parent14fa4dd06c8bd1d6d5ed4a81bf1a70326dbcd95a (diff)
downloadpython-json-patch-f3e067281fbee0829b7bb2656bbef91dffc9146a.tar.gz
allow replacing the root of the document
This fixes the test from https://github.com/json-patch/json-patch-tests/commit/4db5098183c0e1d706b4525
-rw-r--r--jsonpatch.py24
1 files changed, 19 insertions, 5 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index 5633ad1..a2f7973 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -300,7 +300,7 @@ class JsonPatch(object):
for operation in self.patch:
operation = self._get_operation(operation)
- operation.apply(obj)
+ obj = operation.apply(obj)
return obj
@@ -351,6 +351,8 @@ class RemoveOperation(PatchOperation):
except KeyError as ex:
raise JsonPatchConflict(str(ex))
+ return obj
+
class AddOperation(PatchOperation):
"""Adds an object property or an array element."""
@@ -371,12 +373,19 @@ class AddOperation(PatchOperation):
subobj.insert(part, value)
elif isinstance(subobj, dict):
- subobj[part] = value
+ if part is None:
+ # we're replacing the root
+ obj = value
+
+ else:
+ subobj[part] = value
else:
raise JsonPatchConflict("can't add to type '%s'"
"" % subobj.__class__.__name__)
+ return obj
+
class ReplaceOperation(PatchOperation):
"""Replaces an object property or an array element by new value."""
@@ -399,6 +408,7 @@ class ReplaceOperation(PatchOperation):
"" % subobj.__class__.__name__)
subobj[part] = value
+ return obj
class MoveOperation(PatchOperation):
@@ -412,8 +422,9 @@ class MoveOperation(PatchOperation):
if from_ptr.contains(self.pointer):
raise JsonPatchException('Cannot move values into its own children')
- RemoveOperation({'op': 'remove', 'path': self.operation['from']}).apply(obj)
- AddOperation({'op': 'add', 'path': self.location, 'value': value}).apply(obj)
+ obj = RemoveOperation({'op': 'remove', 'path': self.operation['from']}).apply(obj)
+ obj = AddOperation({'op': 'add', 'path': self.location, 'value': value}).apply(obj)
+ return obj
class TestOperation(PatchOperation):
@@ -435,6 +446,8 @@ class TestOperation(PatchOperation):
if val != value:
raise JsonPatchTestFailed('%s is not equal to tested value %s (types %s and %s)' % (val, value, type(val), type(value)))
+ return obj
+
class CopyOperation(PatchOperation):
""" Copies an object property or an array element to a new location """
@@ -443,4 +456,5 @@ class CopyOperation(PatchOperation):
from_ptr = jsonpointer.JsonPointer(self.operation['from'])
subobj, part = from_ptr.to_last(obj)
value = copy.deepcopy(subobj[part])
- AddOperation({'op': 'add', 'path': self.location, 'value': value}).apply(obj)
+ obj = AddOperation({'op': 'add', 'path': self.location, 'value': value}).apply(obj)
+ return obj