test-fullautomation / python-jsonpreprocessor

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

"by reference" vs. "by value" #214

Closed HolQue closed 1 month ago

HolQue commented 4 months ago

In addition to

https://github.com/test-fullautomation/python-jsonpreprocessor/issues/210

I extend the code by a third line in which I recreate testdict2 (instead of testdict1):

${testdict1.subKey1.subKey2} : 1,
"testdict2" : ${testdict1},
${testdict2.subKey1.subKey2} : 2

The result is:

{'testdict1': {'subKey1': {'subKey2': 2}},
 'testdict2': {'subKey1': {'subKey2': 2}}}

The question is: How shall this assignment work:

"testdict2" : ${testdict1},

"by reference" or "by value"?

In pure Python it would be: "by reference". But the JsonPreprocessor code contains a lot of copy.deepcopy() calls. And this is an indicator for "by value".

Because of this makes a difference and because of it is necessary to explain this in documentation and to be able to design self test cases I would prefer to have this clarified: What is the desired value passing method in JsonPreprocessor: "by reference" or "by value"?

If "by reference", the result above is correct. The changed value of subKey2 is visible in both testdict1 and testdict2. But if "by value", the result above is not correct. The expectation in this case would be to have two different objects that are independent from each other:

{'testdict1': {'subKey1': {'subKey2': 1}},
 'testdict2': {'subKey1': {'subKey2': 2}}}
test-fullautomation commented 3 months ago

Hi Son, behaviour should be same like python does. So "by reference" would be required. Thank you, Thomas

namsonx commented 3 months ago

Hello Thomas, Hello Holger,

If we refer "by reference" like Python dose, so there is nothing to do with current behavior of JP as Holger mentioned in ticket.

I use many copy.deepcopy to handle other stuffs such as checking dict or list in for or while loop, I always delete temporary objects from copy.deepcopy once checking completed.

Thank you, Son

HolQue commented 3 months ago

Again I extended the code example from above (by a fourth line). Unfortunately this causes an error.

The following JSON code

${testdict1.subKey1.subKey2} : 1,
"testdict2" : ${testdict1},
${testdict2.subKey1.subKey2} : 2,
${testdict1.subKey1.subKey2} : 3

Result:

{'testdict1': {'subKey1': {'subKey2': 2}},
 'testdict2': {'subKey1': {'subKey2': 2}}}

But I would expect value 3 in both cases (instead of 2).

HolQue commented 3 months ago

No problems with lists:

https://github.com/test-fullautomation/robotframework-testsuitesmanagement/issues/252#issuecomment-1992037429

HolQue commented 2 months ago

Covered by JPP_1900

namsonx commented 2 months ago

Hello Holger,

I fixed and uncovered test case ID JPP_1900, the commit was pushed into stabi branch.

Thank you, Son

HolQue commented 2 months ago

Retest successful. Issue can be closed.

test-fullautomation commented 1 month ago

integrated in RobotFramework AIO 0.11.0