Skip to end of banner
Go to start of banner

Copy of Batch Metadata Expressions

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Introduction

For the wishlist item MWISH-423 - Getting 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 string

  • List<String> is converted into a JSON array

  • Map<String> is converted into a JSON object

  • Map<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

R('Descriptive.Title')

My title

Top

ListField

List of Strings

R('Descriptive.Keywords')

["A", "B", "C"]

Top

MapField

Map of Strings

R('Structural.Versioning')

{"Status": "HEAD", "Version": "2"}

Top

MultiItemField

Map of List of Strings

R('Descriptive.Authors')

{"Authors": ["Cedric"], "Director": ["Alice", "Bob"]}

Top

Others

Not allowed

Sub

Child of ListField

List of Strings

R('Descriptive.Keywords.Keyword')

["A", "B", "C"]

Sub

Child of MultiItemField

List of Strings

R('Descriptive.Authors.Director')

["Alice", "Bob"]

Sub

Child of MapField

String

R('Structural.Versioning.Status')

HEAD

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 invocation

  • Inline lists

  • Ternary operator

  • The method name “R” or “record”

We must whitelist what is allowed, instead of blacklisting what is not allowed

  • No labels