Skip to content

Commit e0b3a9b

Browse files
authored
Merge pull request #134 from Ventilateur/b/fix-invalid-remove-index
B/fix invalid remove index
2 parents a652648 + 46eef55 commit e0b3a9b

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

jsonpatch.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
import functools
4040
import json
4141
import sys
42+
43+
try:
44+
from collections.abc import Sequence
45+
except ImportError: # Python 3
46+
from collections import Sequence
47+
4248
try:
4349
from types import MappingProxyType
4450
except ImportError:
@@ -234,6 +240,10 @@ class RemoveOperation(PatchOperation):
234240

235241
def apply(self, obj):
236242
subobj, part = self.pointer.to_last(obj)
243+
244+
if isinstance(subobj, Sequence) and not isinstance(part, int):
245+
raise JsonPointerException("invalid array index '{0}'".format(part))
246+
237247
try:
238248
del subobj[part]
239249
except (KeyError, IndexError) as ex:

tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def test_remove_array_item(self):
8787
res = jsonpatch.apply_patch(obj, [{'op': 'remove', 'path': '/foo/1'}])
8888
self.assertEqual(res['foo'], ['bar', 'baz'])
8989

90+
def test_remove_invalid_item(self):
91+
obj = {'foo': ['bar', 'qux', 'baz']}
92+
with self.assertRaises(jsonpointer.JsonPointerException):
93+
jsonpatch.apply_patch(obj, [{'op': 'remove', 'path': '/foo/-'}])
94+
95+
9096
def test_replace_object_key(self):
9197
obj = {'foo': 'bar', 'baz': 'qux'}
9298
res = jsonpatch.apply_patch(obj, [{'op': 'replace', 'path': '/baz', 'value': 'boo'}])

0 commit comments

Comments
 (0)