Skip to content

Commit 3c06293

Browse files
authored
Merge pull request #37 from elrandira/master
issue #9: invalid syntax makes Robot crash
2 parents 67e1c9e + e6adc89 commit 3c06293

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

JSONLibrary/JSONLibraryKeywords.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66
from robot.api.deco import keyword
77
from robot.utils.asserts import assert_true, fail
88
from jsonpath_ng import Index, Fields
9-
from jsonpath_ng.ext import parse
9+
from jsonpath_ng.ext import parse as parse_ng
10+
from jsonpath_ng.exceptions import JsonPathParserError
1011
from .version import VERSION
1112

1213
__author__ = 'Traitanit Huangsri'
1314
__email__ = 'traitanit.hua@gmail.com'
1415
__version__ = VERSION
1516

17+
def parse(json_path):
18+
try:
19+
_rv=parse_ng(json_path)
20+
except JsonPathParserError as e:
21+
fail("Parser failed to undestand syntax '{}'. error message: \n{}\n\nYou may raise an issue on https://github.com/h2non/jsonpath-ng".format(json_path,e))
22+
# let other exceptions crash robot
23+
return _rv
24+
1625

1726
class JSONLibraryKeywords(object):
1827
ROBOT_EXIT_ON_FAILURE = True

acceptance/JSONLibrary.robot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ TestShouldNotHaveValueByJSONPath
4848

4949
Run Keyword And Expect Error *Match found* Should Not Have Value In Json ${json_obj} $..isMarried
5050

51+
TestInvalidSyntaxByJSONPath
52+
[Documentation] Check that an invalid syntax fail the test and doesn't crash Robot
53+
${value}= Get Value From Json ${json_obj} $.bankAccounts[?(@.amount>=100)].bank
54+
Should Be Equal As Strings "${value}" "['WesternUnion', 'HSBC']"
55+
56+
${res}= Run Keyword And return status Get Value From Json ${json_obj} $.bankAccounts[?(@.amount=>100)].bank
57+
Should Not Be True ${res}
58+
5159
TestDeleteObjectByJSONPath
5260
[Documentation] Delete object from json object using JSONPath
5361
${json_obj}= Delete Object From Json ${json_obj} $..isMarried

tests/json/example.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
"number": "0123-4567-8999"
2525
}
2626
],
27+
"bankAccounts": [
28+
{
29+
"bank": "WesternUnion",
30+
"amount": "503"
31+
}, {
32+
"bank": "HSBC",
33+
"amount": "1320"
34+
}, {
35+
"bank": "GoldenSack",
36+
"amount": "-10"
37+
}
38+
],
2739
"siblings": [],
2840
"occupation": null
2941
}

tests/test_JSONLibrary.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ def test_delete_all_array_elements_from_json(self):
9999
expected_result = []
100100
self.assertListEqual(expected_result, json_object['phoneNumbers'])
101101

102+
def test_invalid_syntax_doesnt_crash(self):
103+
json_path = '$.bankAccounts[?(@.amount>=100)].bank'
104+
values = self.test.get_value_from_json(self.json, json_path)
105+
expected_result = ['WesternUnion', 'HSBC']
106+
self.assertListEqual(values, expected_result)
107+
108+
json_path = "$.bankAccounts[?(@.amount=>100)].bank"
109+
self.assertRaises(AssertionError, self.test.get_value_from_json, self.json, json_path)
110+
102111
def test_convert_json_to_string(self):
103112
json_str = self.test.convert_json_to_string(self.json)
104113
self.assertTrue(isinstance(json_str, str))

0 commit comments

Comments
 (0)