spring-projects/spring-data-mongodb

Trim BindableMongoExpression input to better support text blocks

gbaso opened this issue · 1 comments

BindableMongoExpression#wrapJsonIfNecessary detects if the input expression already contains the opening and closing braces, and if not they are added. This is useful for one-liners, but it doesn't work as well in other situations.

When using text blocks, it is common to put the closing delimiter on a new line (see the examples in JEP 378 and various tests in this repository), which means the last character of the string will be a line terminator. BindableMongoExpression checks only the last character for }, and if it doesn't match the whole expression is wrapped as { + json + }.

This results in the following:

MongoExpression.create("""
    {
      $toUpper: $last_name
    }
    """);

to be parsed as:

{{
  $toUpper: $last_name
}
}

which is obviously not a valid json. Trimming could be applied to the input expression to avoid this problem.

If the issue is accepted, I can submit a PR.

Thank you for bringing this to our attention. If you have time to work on a PR we're happy to help with that. Please make sure to follow our Contribution Guidelines.