test-fullautomation / python-jsonpreprocessor

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

Parameter name interpreted as regular expression #285

Open HolQue opened 3 weeks ago

HolQue commented 3 weeks ago

It seems that parameter names are interpreted as regular expression immediately:

"+"     : 1,
"param" : ${+}

causes:

'multiple repeat at position 60

"*"     : 3,
"param" : ${*}

causes:

'multiple repeat at position 60

"["     : 21,
"param" : ${[}

causes:

'unterminated character set at position 60


"("     : 23,
"param" : ${(}

causes:

'missing ), unterminated subpattern at position 74

")"     : 24,
"param" : ${)}

causes:

'unbalanced parenthesis at position 60

Later a naming convention check should prevent the users from using such names, but nevertheless: Key names can for sure be computed with the help of regular expressions, but the names themself should not be interpreted as regular expression (what would be a use case for that?).

Similar to (already solved) issue: https://github.com/test-fullautomation/python-jsonpreprocessor/issues/251

HolQue commented 3 weeks ago

Addendum:

(1)

In general: In the context of the JsonPreprocessor a user has no possibility to define regular expressions at any position. In JSON files a user can only define names and values. And he can do this with the help of ${} expressions, but nothing else. Therefore I expect, that a user never will see regular expression error messages like listed above.

Errors w.r.t. regular expressions will therefore be in every case errors made by the developer of the application, but not made by a user.

(2)

In JsonPreprocessor code I see some re.compile() calls, but not encapsulated in a try/except block. This causes very anonymous error messages like

unbalanced parenthesis at position ...

Also a developer may need time now to find the position in the code that causes the error. We should ease this (simply to make the life also for ourselves a little bit easier).

I would prefer something like this:

invalidPattern = "abc)def"
try:
   regexPattern = re.compile(invalidPattern)
except Exception as reason:
   print(f"Exception: '{reason}' in pattern '{invalidPattern}'")

Result:

Exception: 'unbalanced parenthesis at position 3' in pattern 'abc)def'