test-fullautomation / python-jsonpreprocessor

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

Parameter not resolved or not handled properly at lower levels #215

Open HolQue opened 4 months ago

HolQue commented 4 months ago

The following code contains nested dictionaries and lists:

"params" : {"A" : 1,
            "B" : ["C", {"D" : 2,
                         "E" : ["F", 3]
                        }
                  ]
}

Based on this pattern I replace step by step single elements by a parameter.

(1)

"param1" : "X",
"params" : {"A" : 1,
            "${param1}" : ["C", {"D" : 2,
                                 "E" : ["F", 3]}]
}

Result:

{'param1': 'X', 'params': {'A': 1, 'X': ['C', {'D': 2, 'E': ['F', 3]}]}}

OK so far. But the key 'X' is a key created by a parameter. Do we want to allow this here?

(2)

"param1" : "X",
"params" : {"A" : 1,
            "B" : ["C", {"${param1}" : 2,
                                 "E" : ["F", 3]}]
}

Result:

{'param1': 'X',
 'params': {'A': 1, 'B': ['C', {'E': ['F', 3], 'str(${param1})': 2}]}}

Parsing went wrong.

(3)

"param1" : "X",
"params" : {"A" : 1,
            "B" : ["C", {"D" : ${param1},
                         "E" : ["F", 3]}]
}

Result:

{'param1': 'X',
 'params': {'A': 1, 'B': ['C', {'D': '${param1}', 'E': ['F', 3]}]}}

Parsing went wrong.

(4)

"param1" : "X",
"params" : {"A" : 1,
            "B" : ["C", {"D" : "${param1}",
                         "E" : ["F", 3]}]
}

Result:

{'param1': 'X',
 'params': {'A': 1,
            'B': ['C',
                  {'D': '${param1}__ConvertParameterToString__',
                   'E': ['F', 3]}]}}

String conversion failed

(5)

"param1" : "X",
"params" : {"A" : 1,
            "B" : ["C", {"D" : 2,
                         "${param1}" : ["F", 3]}]
}

Result:

{'param1': 'X',
 'params': {'A': 1,
            'B': ['C',
                  {'${param1}__ConvertParameterToString__': ['F', 3], 'D': 2}]}}

Interesting: In (2) "D" has been replaced by the dollar operator expression, instead of "E". And the error message is different. Here it's the second key in the dictionary. In (2) it's the first key. Position dependency?

(6)

"param1" : "X",
"params" : {"A" : 1,
            "B" : ["C", {"D" : 2,
                         "E" : [${param1}, 3]}]
}

Result:

{'param1': 'X', 'params': {'A': 1, 'B': ['C', {'D': 2, 'E': ['${param1}', 3]}]}}

Parsing went wrong.

(7)

"param1" : "X",
"params" : {"A" : 1,
            "B" : ["C", {"D" : 2,
                         "E" : ["${param1}", 3]}]
}

Result:

{'param1': 'X',
 'params': {'A': 1,
            'B': ['C',
                  {'D': 2, 'E': ['${param1}__ConvertParameterToString__', 3]}]}}

String conversion failed

(8)

"param1" : "X",
"params" : {"A" : 1,
            "B" : ["C", {"D" : 2,
                         "E" : ["F", ${param1}]}]
}

Result:

Error: 'Invalid nested parameter format: ${param1}]}] - The double quotes are missing!!!'!

Why this? There are no quotes missing.

(9)

"param1" : "X",
"params" : {"A" : 1,
            "B" : ["C", {"D" : 2,
                         "E" : ["F", "${param1}"]}]
}

Result:

{'param1': 'X',
 'params': {'A': 1, 'B': ['C', {'D': 2, 'E': ['F', 'str(${param1})']}]}}

Parsing went wrong.

(10)

Extended substitution:

"param1" : "X",
"param2" : {"G" : 5, "H" : 6},
"param3" : ["G", "H"],
"params" : {"A" : 1,
            "B" : ["C", {"D" : "ABC ${param2}[${param3}[0]] DEF",
                         "E" : ["F", 3]}]
}

Result:

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

JsonPreprocessor is not able to handle the two successive ']]' in '${param2}[${param3}[0]]'.

(11)

With additional single quotes, to avoid ']]' and to force the explicit string conversion:

"param1" : "X",
"param2" : {"G" : 5, "H" : 6},
"param3" : ["G", "H"],
"params" : {"A" : 1,
            "B" : ["C", {"D" : "ABC ${param2}['${param3}[0]'] DEF",
                         "E" : ["F", 3]}]
}

Result:

Error: 'list index out of range'!

Why is that? There is no list index out of range.

(12)

In dotdict notation:

"param1" : "X",
"param2" : {"G" : 5, "H" : 6, "X" : 9},
"param3" : ["G", "H"],
"params" : {"A" : 1,
            "B" : ["C", {"D" : 2,
                         "E" : [1, "ABC ${param2.${param1}} DEF", 3]
                        }
                  ]
}

Result:

{'param1': 'X',
 'param2': {'G': 5, 'H': 6, 'X': 9},
 'params': {'A': 1,
            'B': ['C',
                  {'D': 2,
                   'E': [1, 'ABC str(${param2.str(${param1})}) DEF', 3]}]}}

Parsing went wrong.

namsonx commented 2 weeks ago

Hello Holger,

For the cases (1), (2), and (5), these cases will raised the exception Exception: A substitution in key names is not allowed!... based on requirement https://github.com/test-fullautomation/python-jsonpreprocessor/issues/270

For other cases you mentioned in this ticket, I already implemented and all cases are working as expected. Could you please help me verify them on stabi branch.

Thank you, Son

HolQue commented 2 weeks ago

Retest successful. Issue can be closed.