test-fullautomation / python-jsonpreprocessor

A preprocessor for json files
Apache License 2.0
2 stars 2 forks source link

Unfavorable order of error detection and parser issue #189

Closed HolQue closed 2 months ago

HolQue commented 5 months ago

Value assignments are usually separated by commas. Every missing comma causes an "Expecting ',' delimiter" error:

"dict_param" : {"A" : 1 , "B" : 2}
"list_param" : ["A", "B", "C"]

(comma is missing at end of first line)

I add a third line. This line has it's own issue: Index placed within curly brackets, instead outside. Additionally the comma is missing at the end at both previous lines:

"dict_param" : {"A" : 1 , "B" : 2}
"list_param" : ["A", "B", "C"]
"val2"       : "${list_param[1]}"

Outcome:

Error: 'Invalid syntax: Found index inside curly brackets in line ...

This error message is correct. But what astonishes me is, that earlier errors (two missing delimiters) are ignored in this case.

The delimiters are used to separate expressions in JSON code. If there is an issue with delimiters, we cannot be sure to have expressions identified correctly (maybe parts are missing or parts are inside that belong to the next expression). If we cannot be sure about the identification of expressions, it's dangerous to make a statement about such expressions. We can have luck and nevertheless the statement is correct (like the error message in the example above), but it also can went wrong and the error message is misleading or wrong completely.

I would prefer to have the error handling realized like I have already explained in https://github.com/test-fullautomation/python-jsonpreprocessor/blob/develop/test/testconfig/Checklist.txt, "Error detection (basics)" and "Parsing strategies", Level 1-4

Please take care of a meaningful order of computation.

Finally I exchanged the parameter val2 by val3. Now the index at wrong position is not hard coded any more, but defined by a dictionary key value. Therefore the expression becomes a little bit more complicated:

"dict_param" : {"A" : 1 , "B" : 2}
"list_param" : ["A", "B", "C"]
"val3"       : "${list_param[${dict_param}['A']]}"

Outcome:

Expecting ',' delimiter

That's fine!

Conclusion: All error messages are correct. But what makes me worried is that the order of the error detection depends on the complexity of the affected expressions. In my opinion this should not happen. Missing delimiters should always have a higher priority in error handling than errors inside expressions that are separated by these delimiters.

Finally I fixed the missing delimiter issue, only the index at wrong position remains:

"dict_param" : {"A" : 1 , "B" : 2},
"list_param" : ["A", "B", "C"],
"val3"       : "${list_param[${dict_param}['A']]}"

Outcome:

Error: 'The variable '${dict_param}['A']]' is not available!'!

This again went wrong completely.

Seems that the parser is not able to handle the two successive closing brackets "]]".

Expected is either

Error: 'Invalid syntax: Found index inside curly brackets

or

Expression '...' cannot be evaluated

HolQue commented 5 months ago

Addendum:

Again an increase of the complexity (val3 -> val4): hard coded "A" exchanged by ${list_param}[0]

"dict_param" : {"A" : 1 , "B" : 2},
"list_param" : ["A", "B", "C"],
"val4"       : "${list_param[${dict_param}[${list_param}[0]]]}"

Outcome:

Error: 'The variable '${dict_param}[${list_param}[0]]]' is not available!'!

Now three successive closing brackets are involved "]]]".

Because of ${list_param}[0] is a string that can represent a key name, it should be possible to put this expression in single quotes (even in case of this is not necessary):

"dict_param" : {"A" : 1 , "B" : 2},
"list_param" : ["A", "B", "C"],
"val4"       : "${list_param[${dict_param}['${list_param}[0]']]}"

Outcome:

Error: 'list index out of range'!

This should not happen. Expected is either

Error: 'Invalid syntax: Found index inside curly brackets

or

Expression '...' cannot be evaluated

To have a good end, let's implement all things in a correct way:

"dict_param" : {"A" : 1 , "B" : 2},
"list_param" : ["A", "B", "C"],
"val5"       : "${list_param}[${dict_param}[${list_param}[0]]]"

The output is like expected!

{'dict_param': {'A': 1, 'B': 2}, 'list_param': ['A', 'B', 'C'], 'val5': 'B'}

namsonx commented 3 months ago

Hello Holger,

Thank your for your detail findings! I have just optimized the way to raise exception in the commit e963c022349 on stabi branch.

Thank you, Son

HolQue commented 3 months ago

Retest successful. Issue can be closed.

test-fullautomation commented 2 months ago

integrated in RobotFramework AIO 0.11.0