Catalog Item Morpheus Variable Evaluation - Unexpected behaviour with "\\"

In Morpheus you may need to be careful when using \\ within the context of Morpheus variable evaluation, as it could result in unexpected behaviour. In the future this might not be necessary, however I am writing this incase anyone else hits this behaviour. On writing this, the latest Morpheus version is 5.4.5-2.

I was recently investigating an issue for a customer where <%= customOptions.group.split('\\|')[0] %> was evaluating to “1” instead of “10” when getting used within a Catalog Item where customOptions.group was equal to “10|VMware”. <%= customOptions.group.split('\\|')[0] %> was correctly evaluating to “10” elsewhere within Morpheus.

When evaluating catalog item config variables, Morpheus does some preemptive escaping for certain character sets. In this case, the \\ characters were being replaced with a specific charset. This was breaking the expected .split() functionality and as such, Groovy was returning each character within “10|VMware” within the returned List. Consequently, customOptions.group.split('\\|')[0] would evaluate to the ID “1”, which was not a group available within the customers environment.

For this use-case, changing .split('\\|') to .split(/[|]/) or .tokenize('|'), will avoid the escaped \\ and can achieve the desired functionality for splitting the output on the | character.

If you are experiencing unexpected behaviour with Variable evaluation and you are using \\, consider using a different syntax to achieve the same goal as \\ may be getting replaced before the Variable is evaluated.

Thanks

2 Likes