Home > other >  Cannot coerce string to boolean in mule4
Cannot coerce string to boolean in mule4

Time:06-02

Getting cannot coerce string to boolean error when running the below expression in mule4

vars.userId != null and (vars.page != null and vars.page contains ('Redeem')) or (vars.actionName != null and vars.actionName contains ('Redeem'))

This is the error:

org.mule.runtime.core.api.expression.ExpressionRuntimeException: "Cannot coerce String (ungatedRedeem) to Boolean
Trace:
  at contains (Unknown)
  at main (line: 1, column: 58)" evaluating expression: "vars.userId != null and (vars.page != null and vars.page contains ('Redeem')) or (vars.actionName != null and vars.actionName contains ('Redeem'))". 

Note: The expression is returning expected output when evaluated separately. Example: when evaluated this expression vars.actionName != null and vars.actionName contains ('Redeem') returning true and when added "vars.userId != null and (vars.page != null and vars.page contains ('Redeem')) or (vars.actionName != null and vars.actionName contains ('Redeem'))" getting error

CodePudding user response:

Short Answer:

Wrap the contains call in its own Parentheses. (vars.page != null) and (vars.page contains ('Redeem')

Explaination

The issue that you are facing is because of the order of precedence of different operator in DataWeave. If you go through the link you will notice that, in your expression and operator will execute before contains

Therefore the expression vars.page != null and vars.page contains ('Redeem') could** throw an error because DataWeave interpret this:

  1. DW first checks if vars.page != null. Lets say it is not so this result to true.
  2. Then it tries to do (vars.page != null and vars.page) or true and vars.page. I believe the vars.page is a String, therefore you are seeing that error because DW is trying to convert String to Boolean.

The resolution is simple. Just wrap the contains call in its own Parentheses. (vars.page != null) and (vars.page contains ('Redeem')

**about that could (also why the other expression is working separately). The reason why the expression vars.actionName != null and vars.actionName contains ('Redeem') worked on its own is because of the logical and and or operators short-circuit, meaning they don't evaluate the remaining parts if they don't have to. So if the first expression vars.actionName != null evaluates to false, then checking the rest of the conditions is not necessary because the result will always be false, so the DW skips the evaluation of the remaining expression. Therefore, depending on the value of actionName your expression could fail. You can read more about short circuiting here. https://en.m.wikipedia.org/wiki/Short-circuit_evaluation

CodePudding user response:

Try adding parenthesis to make precedence explicit. It is considering one of the variable as the and argument before evaluating contains().

(vars.actionName != null and (vars.actionName contains ('Redeem')))

Same for vars.page contains ('Redeem'). Change it to (vars.page contains ('Redeem')).

  • Related