Search code examples
muledataweavemule-elmvel

Unexpected behaviour of MEL function after upgrade to Mule Runtime 3.8.1


I have the following MEL function:

def createSomething(foo){
    if (org.springframework.util.StringUtils.isEmpty(foo)){
        return org.apache.commons.lang.StringUtils.remove(java.util.UUID.randomUUID().toString(), '-');
    }
    if (foo.toString().length() <= 32){
        return foo;
    }
    String fooWithoutHyphens = org.apache.commons.lang.StringUtils.remove(foo.toString(), '-');
    if (fooWithoutHyphens.length() <= 32){
        return fooWithoutHyphens;
    }
    return foo.toString().substring(0, 32);
}

that is being called from a DWL file:

%var mySomething = createSomething("foo")

This is working fine on Mule Runtime 3.7.2.

However after an upgrade to Mule Runtime 3.8.1 I receive the following exception:

com.mulesoft.weave.mule.exception.WeaveExecutionException: Exception while executing: 
Unknown
Not enough arguments (1) for function with parameters (foo, 

fooWithoutHyphens)..
    at com.mulesoft.weave.mule.exception.WeaveExecutionException$.apply(WeaveExecutionException.scala:12)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.execute(WeaveMessageProcessor.scala:121)
    at com.mulesoft.weave.mule.WeaveMessageProcessor.process(WeaveMessageProcessor.scala:67)
    at com.mulesoft.weave.mule.WeaveMessageProcessor$$FastClassByCGLIB$$216b1542.invoke(<generated>)

When I provide anything as a second argument to the function, e.g.

%var mySomething = createSomething("foo", 0)

the exception doesn't occur, although as far as I can tell the function doesn't work as expected.

What is the reason of this behaviour and how can it be fixed?

UPDATE: If the following part:

String fooWithoutHyphens = org.apache.commons.lang.StringUtils.remove(foo.toString(), '-');
if (fooWithoutHyphens.length() <= 32){
    return fooWithoutHyphens;
}

is removed or replaced with:

  if (org.apache.commons.lang.StringUtils.remove(foo.toString(), '-').length() <= 32){
        return org.apache.commons.lang.StringUtils.remove(foo.toString(), '-');
    }

the exception is not thrown. It seems that the declarated string fooWithoutHyphens is now treated as an argument, but I don't know why.


Solution

  • The official doc is succinct on the use of global MEL function, and nowhere is mentioned variable declaration such as String fooWithoutHyphens = .... Related documentation can be found on the MEL Docs and DataWeave docs for 3.8.

    Even if your example worked on Mule 3.7 there's no guarantee it will continue to work on later versions considering it is not documented. I think the work around you used in your example may be a good way to go, or if you require more complex transformation using global MEL function may not be the best way to go.