2

I have been using different forms of Mule's Expression language. I couldn't figure out the difference between

#[flowVars.myVariable]  

and

#[flowVars['myVariable']]

They both give the result when there is a variable. But why do they behave differently when the variable is not present?

Like if the variable being called is not available, then the first expression would result in a exception. Whereas the second expression just gives out a warning or prints out as is, if in a logger message.

Why is this difference?

Also when going through the documentation for Mule 3.6 I found that the second expression is not longer shown in the documentation.

Is the expression #[flowVars['myVariable']] being deprecated?

David Dossot
  • 33,403
  • 4
  • 38
  • 72
user1760178
  • 6,277
  • 5
  • 27
  • 57

2 Answers2

6

The difference comes from the way MVEL deals with these two different ways of accessing map entries.

  • #[flowVars['myVariable']] is equivalent to flowVars.get('myVariable'), which does not fail if the flowVars map does not contain the 'myVariable' entry,
  • #[flowVars.myVariable] treats the flowVars map as a virtual object, leading to an exception if the 'myVariable' entry is missing because in this case it doesn't resolve to a map get but instead to directly using an object member (either a field or a method), which must exist before being accessed.

I don't think #[flowVars['myVariable']] could be deprecated since it's a core feature provided by MVEL.

Reference: http://mvel.codehaus.org/MVEL+2.0+Property+Navigation#MVEL2.0PropertyNavigation-MapAccess

David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • Thanks for the explanation. But there in no mention of the usage of #[flowVars['myVariable']] style in the current (3.6) Mule documentation. http://www.mulesoft.org/documentation/display/current/Mule+Expression+Language+MEL – user1760178 Apr 07 '15 at 21:55
  • But some MEL expressions working in earlier Mule versions are not working in Mule 3.6.1 like #[header:originalFilename] inside a file:inbound-endpoint is failing in mule 3.6.1. – user1760178 Apr 07 '15 at 22:00
  • 1
    `#[header:originalFilename]` is not MEL, it's the old expression format (i.e. 'evaluator:expression') that has been deprecated since Mule 3.3. And I have no idea why the `#[flowVars['myVariable']]` syntax has been removed from Mule's doc... maybe because this is actually an MVEL feature so it doesn't need any specific doc? There are tons of tests in 3.6 that use this syntax: https://github.com/mulesoft/mule/search?utf8=%E2%9C%93&q=%23[flowVars – David Dossot Apr 07 '15 at 22:36
1

David has given a nice explanation around your question. To extend that explanation I would just like to add that you can use #[flowVars.?myVariable] to make your code null safe. This is equivalent to #[flowVars['myVariable']].

Regarding #[header:originalFilename], as David said this is not MEL. You can get a list of non-mel expressions which are commonly used in Mule applications in the following link.

http://www.mulesoft.org/documentation/display/current/Non-MEL+Expressions+Configuration+Reference

Rupesh Sinha
  • 158
  • 1