diff options
-rw-r--r-- | jsonpatch.py | 11 | ||||
-rwxr-xr-x | tests.py | 11 |
2 files changed, 21 insertions, 1 deletions
diff --git a/jsonpatch.py b/jsonpatch.py index 21714c7..d7c1988 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -346,12 +346,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 @@ -485,6 +491,9 @@ class ReplaceOperation(PatchOperation): if part is None: return value + if part == "-": + raise InvalidJsonPatch("'path' with '-' can't be applied to 'replace' operation") + if isinstance(subobj, MutableSequence): if part >= len(subobj) or part < 0: raise JsonPatchConflict("can't replace outside of list") @@ -220,6 +220,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): |