Skip to content

Commit

Permalink
Merge pull request #112 from Alanscut/issue-111
Browse files Browse the repository at this point in the history
fix #111:  optimizing exception message
  • Loading branch information
stefankoegl authored Jun 22, 2020
2 parents 625555e + 1015d7f commit 4fe5c2c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion jsonpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,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

Expand Down
11 changes: 11 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ def test_append(self):
])
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):
Expand Down

0 comments on commit 4fe5c2c

Please sign in to comment.