We are migrating a Mule 3 api to Mule 4 and am observing some behaviour which am not able to make sense of in Mule 3 .....
We have an incoming payload from an HTTP endpoint
{
empId: 10,
empAge: 23,
empName: "Xyz",
bonus: "N",
performance: "Good",
reward: "N"
}
which we are storing in a variable:
<set-variable variableName="originalPayload" value="#[payload]" doc:name="Store original payload"/>
We then have a Groovy component that is performing some logic for bonus:
<scripting:script engine="Groovy"><![CDATA[
if (flowVars.originalPayload.empAge > 20) {
flowVars.originalPayload.put("bonus", "Y")
}
return payload]]>
</scripting:script>
</scripting:component>
Here I can see that 'bonus' is now set to 'Y' in both the originalPayload
( expected ) and also in the payload
( not expected )
The above happens after the execution of Groovy component.
Further into the flow - we are using an expression component
:
<expression-component doc:name="Set reward">
<![CDATA[if (payload.performance == "Good") { payload.reward = "Y" }]]>
</expression-component>
after code executes the above expression as well reward
is now reflecting as 'Y' in both payload ( expected ) and also iin flowVars.originalPayload
( Not expected )
In Mule 4 am replacing Groovy and expression with Dataweave transform component and the changes done for bonus
reflect only in originalPayload
( expected ) but not in payload
. Similarly reward
in Mule 4 is now reflecting as 'Y' ONLY in payload
( expected ) and NOT in vars.originalPayload
so in short Mule 4 behaviour makes sense but not the Mule 3 behaviour .
Can someone please explain why we see this behaviour in Mule 3 ?
Same as Java -Mule is developed in Java- the variables and payload the payload and flow variables are object references, because they are Java objects. Thus when you are assigning the payload to a variable both will be reference to the same object instance. Your payload seems to be a Map. If you change the value of a key using either reference they are accessing the same object.
DataWeave -in both Mule versions- always generates a new output object, so the references will be different.