Skip to content

Commit d856664

Browse files
gbasochristophstrobl
authored andcommitted
Trim BindlableMongoExpression input.
Trim the given source so that wrapping checks still work for text blocks. Fix a typo in Javadoc. Resolves: #4821 Original Pull Request: #4822
1 parent 5015fff commit d856664

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/BindableMongoExpression.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec;
2424
import org.springframework.data.util.Lazy;
2525
import org.springframework.lang.Nullable;
26+
import org.springframework.util.Assert;
2627
import org.springframework.util.ObjectUtils;
2728
import org.springframework.util.StringUtils;
2829

2930
/**
3031
* A {@link MongoExpression} using the {@link ParameterBindingDocumentCodec} for parsing a raw ({@literal json})
3132
* expression. The expression will be wrapped within <code>{ ... }</code> if necessary. The actual parsing and parameter
32-
* binding of placeholders like {@code ?0} is delayed upon first call on the the target {@link Document} via
33+
* binding of placeholders like {@code ?0} is delayed upon first call on the target {@link Document} via
3334
* {@link #toDocument()}.
3435
* <br />
3536
*
@@ -45,6 +46,7 @@
4546
* containing the required {@link org.bson.codecs.Codec codec} via {@link #withCodecRegistry(CodecRegistry)}.
4647
*
4748
* @author Christoph Strobl
49+
* @author Giacomo Baso
4850
* @since 3.2
4951
*/
5052
public class BindableMongoExpression implements MongoExpression {
@@ -77,7 +79,9 @@ public BindableMongoExpression(String expression, @Nullable Object[] args) {
7779
public BindableMongoExpression(String expression, @Nullable CodecRegistryProvider codecRegistryProvider,
7880
@Nullable Object[] args) {
7981

80-
this.expressionString = expression;
82+
Assert.notNull(expression, "Expression must not be null");
83+
84+
this.expressionString = expression.trim();
8185
this.codecRegistryProvider = codecRegistryProvider;
8286
this.args = args;
8387
this.target = Lazy.of(this::parse);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateFieldProjectionTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*
4343
* @author Christoph Strobl
4444
* @author Mark Paluch
45+
* @author Giacomo Baso
4546
*/
4647
@ExtendWith(MongoTemplateExtension.class)
4748
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.4")
@@ -89,6 +90,21 @@ void usesMongoExpressionWithPlaceholdersAsIs() {
8990
assertThat(result).isEqualTo(luke.upperCaseLastnameClone());
9091
}
9192

93+
@Test // GH-4821
94+
void usesMongoExpressionWithLineBreaksAsIs() {
95+
96+
Person result = findLuke(fields -> {
97+
fields.include("firstname").project(MongoExpression.create("""
98+
{
99+
'$toUpper' : '$last_name'
100+
}
101+
"""))
102+
.as("last_name");
103+
});
104+
105+
assertThat(result).isEqualTo(luke.upperCaseLastnameClone());
106+
}
107+
92108
@Test // GH-3583
93109
void mapsAggregationExpressionToDomainType() {
94110

0 commit comments

Comments
 (0)