spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.08k forks source link

Trim BindableMongoExpression input to better support text blocks #4821

Open gbaso opened 1 week ago

gbaso commented 1 week ago

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.

christophstrobl commented 4 days ago

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.