Skip to content

Commit 4fe5c2c

Browse files
authored
Merge pull request #112 from Alanscut/issue-111
fix #111: optimizing exception message
2 parents 625555e + 1015d7f commit 4fe5c2c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

jsonpatch.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,18 @@ class PatchOperation(object):
334334

335335
def __init__(self, operation):
336336

337+
if not operation.__contains__('path'):
338+
raise InvalidJsonPatch("Operation must have a 'path' member")
339+
337340
if isinstance(operation['path'], JsonPointer):
338341
self.location = operation['path'].path
339342
self.pointer = operation['path']
340343
else:
341344
self.location = operation['path']
342-
self.pointer = JsonPointer(self.location)
345+
try:
346+
self.pointer = JsonPointer(self.location)
347+
except TypeError as ex:
348+
raise InvalidJsonPatch("Invalid 'path'")
343349

344350
self.operation = operation
345351

tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,17 @@ def test_append(self):
219219
])
220220
self.assertEqual(res['foo'], [1, 2, 3, 4])
221221

222+
def test_add_missing_path(self):
223+
obj = {'bar': 'qux'}
224+
self.assertRaises(jsonpatch.InvalidJsonPatch,
225+
jsonpatch.apply_patch,
226+
obj, [{'op': 'test', 'value': 'bar'}])
227+
228+
def test_path_with_null_value(self):
229+
obj = {'bar': 'qux'}
230+
self.assertRaises(jsonpatch.InvalidJsonPatch,
231+
jsonpatch.apply_patch,
232+
obj, '[{"op": "add", "path": null, "value": "bar"}]')
222233

223234

224235
class EqualityTestCase(unittest.TestCase):

0 commit comments

Comments
 (0)