Search code examples
anypoint-studiomule-esbmule4

Mule4 when expression failing for json array "contains" check


Working on Mule 3.9 to Mule 4.x migration work, API recieve json input payload as shown below, I am trying to check whether I have DFU string value existing in this array payload.sec_entities. Observed that in log statement condition check is working perfectly fine, see the log statement showing debug-8, debug-9, debug-10 below in Error logs. Question is it's not working same statement in when expression Can anyone suggest what is the issue with when expresion??

Tried with this, when expression fails:

<when expression="#[payload.sec_entities != null and payload.sec_entities contains 'DFU']">

Tried with this, even this when expression fails:

<when expression="#[%dw 2.0 output application/java --- payload.sec_entities != null and payload.sec_entities contains 'DFU' as String]">

Input payload:

{
  "primary_entities": [
    "DMDUNIT",
    "LOC"
  ],
  "sec_entities": [
    "DFU",
    "STOCK"
  ]
}

Code:

<flow name="Orchestrator-DFU-Flow">
        <logger message="debug-8 expression check: payload.sec_entities: #[payload.sec_entities]"/>
        <logger message="debug-9 expression check: payload.sec_entities not null check: #[payload.sec_entities != null]"/>
        <logger message="debug-10 expression check: payload.sec_entities contains: #[payload.sec_entities contains 'DFU']"/>
        <choice doc:name="Choice">
            <when expression="#[%dw 2.0 output application/java --- payload.sec_entities != null and payload.sec_entities contains 'DFU' as String]">
                <set-payload value="DFU" doc:name="Set Payload" />
                <set-variable variableName="filterJson" value="#[%dw 2.0 
import java!com::jda::cd::util::StringUtil
output application/json 
--- 
StringUtil::getFilterExpression(vars.modifiedJsonInPayload, payload)]" doc:name="Set dfuFilterJson Session Variable" doc:id="8f20824e-b4eb-431c-a2e3-bdaf155f3367"/>
                <set-variable variableName="entityMapForBatch" value='#[{"sec_entities":[payload]}]' doc:name="Variable - entityMapForBatch" doc:id="928fa7d9-b620-4e9f-9a30-caea11dd5d04"/>
                <logger message="#['Posting Filter $(payload)']" level="INFO" doc:name="Log_DFU_Filter" />
                <flow-ref name="Create_Batch" doc:name="Create_Batch" />
                <flow-ref name="post_masterdata_to_messaging_system" doc:name="post_masterdata_to_messaging_system" />
            </when>
            <otherwise>
                <logger level="INFO" doc:name="Logger" message="DFU flows will not be triggered." />
            </otherwise>
        </choice>

        <error-handler ref="Global_Errorflow_Choice_Exception_Strategy" doc:name="Reference Exception Strategy" />
    </flow>

Error logs:

INFO  2019-04-03 18:47:20,113 [[MuleRuntime].cpuLight.11: [jda-demand-adapter].Orchestrator-DFU-Flow.CPU_LITE @5011b8c6] [event: 0-cd4e4691-5612-11e9-ae40-0a0027000005] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: debug-8 expression check: payload.sec_entities: ["DFU","STOCK"]
INFO  2019-04-03 18:53:30,181 [[MuleRuntime].cpuLight.09: [jda-demand-adapter].Orchestrator-DFU-Flow.CPU_LITE @36218376] [event: 0-aaaca7c1-5613-11e9-ae40-0a0027000005] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: debug-9 expression check: payload.sec_entities not null check: true
INFO  2019-04-03 18:53:30,183 [[MuleRuntime].cpuLight.09: [jda-demand-adapter].Orchestrator-DFU-Flow.CPU_LITE @36218376] [event: 0-aaaca7c1-5613-11e9-ae40-0a0027000005] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: debug-10 expression check: payload.sec_entities contains: true
ERROR 2019-04-03 18:53:30,187 [[MuleRuntime].cpuLight.09: [jda-demand-adapter].Orchestrator-DFU-Flow.CPU_LITE @36218376] [event: 0-aaaca7c1-5613-11e9-ae40-0a0027000005] org.mule.runtime.core.internal.exception.OnErrorContinueHandler: 
********************************************************************************
Message               : "Cannot coerce Array (["DFU", "STOCK"]) to Boolean
Trace:
  at main (line: 1, column: 91)" evaluating expression: "%dw 2.0 output application/java --- payload.sec_entities != null and payload.sec_entities contains 'DFU' as String".
Error type            : MULE:EXPRESSION
Element               : Orchestrator-DFU-Flow/processors/3 @ jda-demand-adapter:orchestrator-flow.xml:125 (Choice)
Element XML           : <choice doc:name="Choice">
<when expression="#[%dw 2.0 output application/java --- payload.sec_entities != null and payload.sec_entities contains 'DFU' as String]">

Solution

  • Found the solution for above issue:

    <when expression="#[output application/json
    --- 
    ((payload.sec_entities != null) and (payload.sec_entities contains 'DFU'))]">