summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2020-06-22 14:43:51 +0200
committerGitHub <noreply@github.com>2020-06-22 14:43:51 +0200
commit4fe5c2c6a1e082fd1bacd36d8862d3a4d968f20b (patch)
tree4a87ff52eea268f47101866ceae9f42c0fe5f81a
parent625555ef5720d0060b7eeb477bfe5a693d4b25ef (diff)
parent1015d7f35958f196478f32148656171f87358cde (diff)
downloadpython-json-patch-4fe5c2c6a1e082fd1bacd36d8862d3a4d968f20b.tar.gz
Merge pull request #112 from Alanscut/issue-111
fix #111: optimizing exception message
-rw-r--r--jsonpatch.py8
-rwxr-xr-xtests.py11
2 files changed, 18 insertions, 1 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index e042ce2..7d5489a 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -334,12 +334,18 @@ class PatchOperation(object):
def __init__(self, operation):
+ if not operation.__contains__('path'):
+ raise InvalidJsonPatch("Operation must have a 'path' member")
+
if isinstance(operation['path'], JsonPointer):
self.location = operation['path'].path
self.pointer = operation['path']
else:
self.location = operation['path']
- self.pointer = JsonPointer(self.location)
+ try:
+ self.pointer = JsonPointer(self.location)
+ except TypeError as ex:
+ raise InvalidJsonPatch("Invalid 'path'")
self.operation = operation
diff --git a/tests.py b/tests.py
index cde90b0..0abf4d2 100755
--- a/tests.py
+++ b/tests.py
@@ -219,6 +219,17 @@ class ApplyPatchTestCase(unittest.TestCase):
])
self.assertEqual(res['foo'], [1, 2, 3, 4])
+ def test_add_missing_path(self):
+ obj = {'bar': 'qux'}
+ self.assertRaises(jsonpatch.InvalidJsonPatch,
+ jsonpatch.apply_patch,
+ obj, [{'op': 'test', 'value': 'bar'}])
+
+ def test_path_with_null_value(self):
+ obj = {'bar': 'qux'}
+ self.assertRaises(jsonpatch.InvalidJsonPatch,
+ jsonpatch.apply_patch,
+ obj, '[{"op": "add", "path": null, "value": "bar"}]')
class EqualityTestCase(unittest.TestCase):