test-fullautomation / python-jsonpreprocessor

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

Parsing harmed by special characters #278

Open HolQue opened 1 month ago

HolQue commented 1 month ago

Code:

"param" : {"key" : "${A}"}

It is detected properly that a parameter with name 'A' does not exist:

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

Instead of 'A' I put inside some special characters:

"param" : {"key" : "${+}"}
"param" : {"key" : "${$}"}
"param" : {"key" : "${%}"}
"param" : {"key" : "${&}"}
"param" : {"key" : "${*}"}

Every line causes this error:

Expecting ',' delimiter: line ...

(every line tested separately!!)

Why this? Looks like a syntax error. Seems that the JsonPreprocessor is harmed by these special characters inside names.

But I would not expect this. I see two possibilities:

HolQue commented 1 month ago

This is really interesting. In https://github.com/test-fullautomation/python-jsonpreprocessor/issues/279 I described a way to get access to the JSON document being parsed.

When I check now the lines above, e.g.

"param" : {"key" : "${%}"}

then I get:

Expecting ',' delimiter: line 2 column 25 (char 26)
Nearby: '... {\n   "param" : {"key" : ""${%}"}"\n}\n ...''!

Striking point: ""${%}"}"

It can be seen that additional quotes are present. This makes the 'Expecting ',' delimiter' error plausible.

Seems that the JSON document access is also a good debugging mechanism.

But nevertheless: From where do the additional quotes come from?

HolQue commented 1 month ago

Maybe similar:

"listP"  : ["A", "B"],
"params" : [[[${listP}[${IAMNOTEXISTING}], 123], ${listP}[${IAMNOTEXISTING}]], ${listP}[${IAMNOTEXISTING}]]

Result:

Expecting ':' delimiter: line 3 column 82 (char 109)
Nearby: '... NG}]",123],"${listP}[${IAMNOTEXISTING}]"],"${listP ...''

Observation: Also in this case additional quotes (}]"]) that are not expected.

HolQue commented 1 month ago

Or how about this?

...
"param4" : {"key2" : [${listParam}[0], {"key3" : ${listParam}[-${index}]}]},
"${listParam}[2]" : 4

Result:

Expecting ',' delimiter: line 12 column 1 (char 363)
Nearby: '... }[2]__ConvertParameterToString__" : 4\n}\n ...''

This means you still have your token __ConvertParameterToString__ inside the code you pass to the JSON interface.

This also means it is urgently required to make sure that the JSON code is valid and does not contain any additional quotes or token strings any more, before you pass this code to the JSON interface. Because the things described above happen behind the curtain. No user usually will see, what the JSON interface see. And therefore the user has no chance to find the real root cause for errors like Expecting ',' delimiter in case of expressions are not resolved completely before.

To stabilize this aspect of the JsonPreprocessor would really be a huge step and will make a lot of very user unfriendly error messages disappear.

namsonx commented 3 weeks ago

Hello Holger,

This ticket was solved in my last improvement. Could you please verify again.

Thank you, Son

HolQue commented 1 week ago

Retest successful. Issue can be closed.