diff options
author | Stefan Kögl <stefan@skoegl.net> | 2021-04-12 21:03:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-12 21:03:29 +0200 |
commit | e0b3a9b1fd57bbf9095163dc8c65dd64897b7ca2 (patch) | |
tree | e14fd9a915d0c4ac64a213ff5f0ad0161779e848 | |
parent | a6526489c4b741e1b603b5bb988ecc9aefd3e880 (diff) | |
parent | 46eef55d5170c08dd9513c86703b365f3d51db3c (diff) | |
download | python-json-patch-e0b3a9b1fd57bbf9095163dc8c65dd64897b7ca2.tar.gz |
Merge pull request #134 from Ventilateur/b/fix-invalid-remove-index
B/fix invalid remove index
-rw-r--r-- | jsonpatch.py | 10 | ||||
-rwxr-xr-x | tests.py | 6 |
2 files changed, 16 insertions, 0 deletions
diff --git a/jsonpatch.py b/jsonpatch.py index 1bced46..a4bd519 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -39,6 +39,12 @@ import copy import functools import json import sys + +try: + from collections.abc import Sequence +except ImportError: # Python 3 + from collections import Sequence + try: from types import MappingProxyType except ImportError: @@ -234,6 +240,10 @@ class RemoveOperation(PatchOperation): def apply(self, obj): subobj, part = self.pointer.to_last(obj) + + if isinstance(subobj, Sequence) and not isinstance(part, int): + raise JsonPointerException("invalid array index '{0}'".format(part)) + try: del subobj[part] except (KeyError, IndexError) as ex: @@ -87,6 +87,12 @@ class ApplyPatchTestCase(unittest.TestCase): res = jsonpatch.apply_patch(obj, [{'op': 'remove', 'path': '/foo/1'}]) self.assertEqual(res['foo'], ['bar', 'baz']) + def test_remove_invalid_item(self): + obj = {'foo': ['bar', 'qux', 'baz']} + with self.assertRaises(jsonpointer.JsonPointerException): + jsonpatch.apply_patch(obj, [{'op': 'remove', 'path': '/foo/-'}]) + + def test_replace_object_key(self): obj = {'foo': 'bar', 'baz': 'qux'} res = jsonpatch.apply_patch(obj, [{'op': 'replace', 'path': '/baz', 'value': 'boo'}]) |