Skip to content

Commit c9bfb91

Browse files
committed
FIX: TypeError when one forgot to put its operation in a list.
1 parent 55d4816 commit c9bfb91

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

jsonpatch.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,19 @@ def __init__(self, patch, pointer_cls=JsonPointer):
558558
# Much of the validation is done in the initializer
559559
# though some is delayed until the patch is applied.
560560
for op in self.patch:
561+
# We're only checking for basestring in the following check
562+
# for two reasons:
563+
#
564+
# - It should come from JSON, which only allows strings as
565+
# dictionary keys, so having a string here unambiguously means
566+
# someone used: {"op": ..., ...} instead of [{"op": ..., ...}].
567+
#
568+
# - There's no possible false positive: if someone give a sequence
569+
# of mappings, this won't raise.
570+
if isinstance(op, basestring):
571+
raise InvalidJsonPatch("Document is expected to be sequence of "
572+
"operations, got a sequence of strings.")
573+
561574
self._get_operation(op)
562575

563576
def __str__(self):
@@ -677,7 +690,7 @@ def _get_operation(self, operation):
677690
op = operation['op']
678691

679692
if not isinstance(op, basestring):
680-
raise InvalidJsonPatch("Operation must be a string")
693+
raise InvalidJsonPatch("Operation's op must be a string")
681694

682695
if op not in self.operations:
683696
raise InvalidJsonPatch("Unknown operation {0!r}".format(op))

tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ def test_test_not_existing(self):
190190
obj, [{'op': 'test', 'path': '/baz', 'value': 'bar'}])
191191

192192

193+
def test_forgetting_surrounding_list(self):
194+
obj = {'bar': 'qux'}
195+
self.assertRaises(jsonpatch.InvalidJsonPatch,
196+
jsonpatch.apply_patch,
197+
obj, {'op': 'test', 'path': '/bar'})
198+
193199
def test_test_noval_existing(self):
194200
obj = {'bar': 'qux'}
195201
self.assertRaises(jsonpatch.InvalidJsonPatch,

0 commit comments

Comments
 (0)