summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Kral <william.kral@gmail.com>2013-06-23 14:36:19 -0700
committerWilliam Kral <william.kral@gmail.com>2013-06-23 14:36:19 -0700
commit3b6a5eefb5f6052db7e46a45baa9f3c6c9960022 (patch)
treee8c6d7e459e3481cee705baae98a718893150c9c
parente58df080ebc5866da64322b6b9d121ceb16683c5 (diff)
downloadpython-json-patch-3b6a5eefb5f6052db7e46a45baa9f3c6c9960022.tar.gz
Fixed replacing whole document
- json pointer to_last operation returns None for the part in the case that it is a whole document pointer - json patch now checks to see if the part is None and simply returns the value to replace the document - Added a test to verify the fix
-rw-r--r--jsonpatch.py3
-rwxr-xr-xtests.py5
2 files changed, 8 insertions, 0 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index e115273..5c61a0d 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -394,6 +394,9 @@ class ReplaceOperation(PatchOperation):
value = self.operation["value"]
subobj, part = self.pointer.to_last(obj)
+ if part is None:
+ return value
+
if isinstance(subobj, list):
if part > len(subobj) or part < 0:
raise JsonPatchConflict("can't replace outside of list")
diff --git a/tests.py b/tests.py
index d6506e1..89e5274 100755
--- a/tests.py
+++ b/tests.py
@@ -56,6 +56,11 @@ class ApplyPatchTestCase(unittest.TestCase):
res = jsonpatch.apply_patch(obj, [{'op': 'replace', 'path': '/baz', 'value': 'boo'}])
self.assertTrue(res['baz'], 'boo')
+ def test_replace_whole_document(self):
+ obj = {'foo': 'bar'}
+ res = jsonpatch.apply_patch(obj, [{'op': 'replace', 'path': '', 'value': {'baz': 'qux'}}])
+ self.assertTrue(res['baz'], 'qux')
+
def test_replace_array_item(self):
obj = {'foo': ['bar', 'qux', 'baz']}
res = jsonpatch.apply_patch(obj, [{'op': 'replace', 'path': '/foo/1',