Foreach is not working with Variables
Description
I built this integration where I loop through an array in a variable using foreach.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/sample" name="sample" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" uri-template="/">
<inSequence>
<variable name="arrayVar" type="JSON" value="{"array":[{"id":"011"},{"id":"012"}]}
"/>
<foreach collection="${vars.arrayVar.array}" parallel-execution="true" update-original="true" continue-without-aggregation="false">
<sequence>
<log category="INFO" logMessageID="false" logFullPayload="false">
<message>${payload.id}</message>
</log>
</sequence>
</foreach>
<respond/>
</inSequence>
<faultSequence>
</faultSequence>
</resource>
</api>
While the array values are being looped through during execution, the flow doesn't end correctly—the respond mediator is not being triggered.
Here is the error I'm getting in the output
[2025-05-07 22:48:00,708] INFO {PassThroughListeningIOReactorManager} - Pass-through EI_INTERNAL_HTTP_INBOUND_ENDPOINT Listener started on 0.0.0.0:9201
[2025-05-07 22:48:00,723] INFO {PassThroughListeningIOReactorManager} - Pass-through EI_INTERNAL_HTTPS_INBOUND_ENDPOINT Listener started on 0.0.0.0:9164
[2025-05-07 22:48:00,723] INFO {StartupFinalizer} - WSO2 Micro Integrator started in 3.45 seconds
[2025-05-07 22:48:05,033] INFO {AuthenticationHandlerAdapter} - User admin logged in successfully
[2025-05-07 22:48:08,834] INFO {LogMediator} - {api:sample GET /sample/} 011
[2025-05-07 22:48:08,834] INFO {LogMediator} - {api:sample GET /sample/} 012
[2025-05-07 22:48:08,837] ERROR {MediatorWorker} - {api:sample} Unexpected error executing task/async inject com.jayway.jsonpath.PathNotFoundException
Exception in thread "SynapseWorker-1" java.lang.NullPointerException: Cannot invoke "String.split(String)" because the return value of "java.lang.Exception.getMessage()" is null
at org.apache.synapse.FaultHandler.handleFault(FaultHandler.java:85)
at org.apache.synapse.mediators.MediatorWorker.run(MediatorWorker.java:134)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1583)
Steps to Reproduce
- Download the MI Runtime 4.4.0 and update to the latest update.
- Create a new project and API called "sample".
- Paste the following and try to run.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/sample" name="sample" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" uri-template="/">
<inSequence>
<variable name="arrayVar" type="JSON" value="{"array":[{"id":"011"},{"id":"012"}]}
"/>
<foreach collection="${vars.arrayVar.array}" parallel-execution="true" update-original="true" continue-without-aggregation="false">
<sequence>
<log category="INFO" logMessageID="false" logFullPayload="false">
<message>${payload.id}</message>
</log>
</sequence>
</foreach>
<respond/>
</inSequence>
<faultSequence>
</faultSequence>
</resource>
</api>
Version
Runtime Version: 4.4.0
Environment Details (with versions)
WSO2 MI VS Code Extension Version : 2.2.0
Hi, I was able to recreate this and it looks interesting. Could you please assign it to me?
Hi @RDPerera , I would like to work on this issue. Could you please assign it to me?
Hi @RDPerera, I figured out the solution for it, so could you please assign to me so i can contribute to this amazing project. under Hacktoberfest 2025
Hi @RDPerera / @chanikag , I’ve recreated this issue in my local system and identified the solution. Kindly assign it to me, and I will resolve it as soon as possible.
Hi @RDPerera / @chanikag,
I’ve reviewed the issue and was able to reproduce the behavior locally. I’d be glad to work on this and help provide a proper fix for the foreach mediator when used with variables.
If this issue is still open for contribution, could you please assign it to me?
Thank you!