micro-integrator icon indicating copy to clipboard operation
micro-integrator copied to clipboard

Foreach is not working with Variables

Open RDPerera opened this issue 7 months ago • 5 comments

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="{&quot;array&quot;:[{&quot;id&quot;:&quot;011&quot;},{&quot;id&quot;:&quot;012&quot;}]}
"/>
            <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.

Image

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

  1. Download the MI Runtime 4.4.0 and update to the latest update.
  2. Create a new project and API called "sample".
  3. 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="{&quot;array&quot;:[{&quot;id&quot;:&quot;011&quot;},{&quot;id&quot;:&quot;012&quot;}]}
"/>
            <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

RDPerera avatar May 07 '25 17:05 RDPerera

Hi, I was able to recreate this and it looks interesting. Could you please assign it to me?

PularaW avatar Sep 23 '25 10:09 PularaW

Hi @RDPerera , I would like to work on this issue. Could you please assign it to me?

vimukthiwaththegama avatar Sep 24 '25 05:09 vimukthiwaththegama

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

Lahu19 avatar Sep 29 '25 15:09 Lahu19

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.

myleshmulecraft avatar Sep 30 '25 07:09 myleshmulecraft

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!

adityagupta0251 avatar Sep 30 '25 17:09 adityagupta0251