Browse Source

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
4.4.x
Giacomo Baso 1 year ago committed by Christoph Strobl
parent
commit
486bf84a7d
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 8
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/BindableMongoExpression.java
  2. 16
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateFieldProjectionTests.java

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

@ -23,13 +23,14 @@ import org.bson.codecs.configuration.CodecRegistry;
import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec; import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec;
import org.springframework.data.util.Lazy; import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* A {@link MongoExpression} using the {@link ParameterBindingDocumentCodec} for parsing a raw ({@literal json}) * A {@link MongoExpression} using the {@link ParameterBindingDocumentCodec} for parsing a raw ({@literal json})
* expression. The expression will be wrapped within <code>{ ... }</code> if necessary. The actual parsing and parameter * expression. The expression will be wrapped within <code>{ ... }</code> if necessary. The actual parsing and parameter
* binding of placeholders like {@code ?0} is delayed upon first call on the the target {@link Document} via * binding of placeholders like {@code ?0} is delayed upon first call on the target {@link Document} via
* {@link #toDocument()}. * {@link #toDocument()}.
* <br /> * <br />
* *
@ -45,6 +46,7 @@ import org.springframework.util.StringUtils;
* containing the required {@link org.bson.codecs.Codec codec} via {@link #withCodecRegistry(CodecRegistry)}. * containing the required {@link org.bson.codecs.Codec codec} via {@link #withCodecRegistry(CodecRegistry)}.
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Giacomo Baso
* @since 3.2 * @since 3.2
*/ */
public class BindableMongoExpression implements MongoExpression { public class BindableMongoExpression implements MongoExpression {
@ -77,7 +79,9 @@ public class BindableMongoExpression implements MongoExpression {
public BindableMongoExpression(String expression, @Nullable CodecRegistryProvider codecRegistryProvider, public BindableMongoExpression(String expression, @Nullable CodecRegistryProvider codecRegistryProvider,
@Nullable Object[] args) { @Nullable Object[] args) {
this.expressionString = expression; Assert.notNull(expression, "Expression must not be null");
this.expressionString = expression.trim();
this.codecRegistryProvider = codecRegistryProvider; this.codecRegistryProvider = codecRegistryProvider;
this.args = args; this.args = args;
this.target = Lazy.of(this::parse); this.target = Lazy.of(this::parse);

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

@ -42,6 +42,7 @@ import org.springframework.data.mongodb.test.util.Template;
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch * @author Mark Paluch
* @author Giacomo Baso
*/ */
@ExtendWith(MongoTemplateExtension.class) @ExtendWith(MongoTemplateExtension.class)
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.4") @EnableIfMongoServerVersion(isGreaterThanEqual = "4.4")
@ -89,6 +90,21 @@ class MongoTemplateFieldProjectionTests {
assertThat(result).isEqualTo(luke.upperCaseLastnameClone()); assertThat(result).isEqualTo(luke.upperCaseLastnameClone());
} }
@Test // GH-4821
void usesMongoExpressionWithLineBreaksAsIs() {
Person result = findLuke(fields -> {
fields.include("firstname").project(MongoExpression.create("""
{
'$toUpper' : '$last_name'
}
"""))
.as("last_name");
});
assertThat(result).isEqualTo(luke.upperCaseLastnameClone());
}
@Test // GH-3583 @Test // GH-3583
void mapsAggregationExpressionToDomainType() { void mapsAggregationExpressionToDomainType() {

Loading…
Cancel
Save