Introduction
For the wishlist item - MWISH-423Getting issue details... STATUS we want to introducte an expression language in order the describe the dynamical task requested by the customer.
Requirements
When executing a batch the new metadata of the item should be able to be determined by
The previous/current metadata of the item
Combinations of fields
Conditions
Textual operations
Solution
We create a new batch action “Metadata Expression” which takes as an argument a metadata sidecar e.g.
{ "Descriptive": { "Title": "Metadata Expression Analysis" }, "Dynamic": { "CustomField": "Some value" } }
This metadata sidecar can now use a strict Spring Expression Language (Spel) subset to perform more complex operations. You can put one or more expressions #{...}
in the JSON or XML. Inside an expression, you can reference the metadata using R(<Dotted key>)
or record(<Dotted key>)
for example R('Descriptive.Title')
or record('Descriptive.Title')
.
The system ensures the result after evaluating the expression it is correctly escaped
Example input
{ "Descriptive": { "Title": "R('Descriptive.Title').toUpperCase()", "Description": "R('Descriptive.Title') + ': ' + R('Dynamic.CustomField')", "Keywords": {"Keyword": "{R('Dynamic.CustomField')}"} }, "Dynamic": { "CustomField": "R('Dynamic.CustomField2') == 'X' ? 'Option A' : 'Option B'" } }
The expression for each JSON property must evaluate to one of the following types
String
is converted into a JSON stringList<String>
is converted into a JSON arrayMap<String>
is converted into a JSON objectMap<String,List<String>>
is converted into a JSON object containing JSON arrays
All special characters are automatically escaped
The output of the metadata accessor
The output of the metadata accessor R
or record
is the following depending on which field type the dotted key refers to:
Level | Case | Effect | Example Expression | Example Output |
---|---|---|---|---|
Top | SimpleField | String |
|
|
Top | ListField | List of Strings |
|
|
Top | MapField | Map of Strings |
|
|
Top | MultiItemField | Map of List of Strings |
|
|
Top | Others | Not allowed | ||
Sub | Child of ListField | List of Strings |
|
|
Sub | Child of MultiItemField | List of Strings |
|
|
Sub | Child of MapField | String |
|
|
Sub | Other | Not allowed |
The R
takes an optional second argument, which is the default value. When it is not specified the default value is the empty string.
Security
The available features of SPel are described here: https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/expressions.html. We must disable all features except
Literal expressions
Boolean and relational operators
Method invocationInline lists
Ternary operator
The method name “R” or “record”
We must whitelist what is allowed, instead of blacklisting what is not allowed