Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim BindlableMongoExpression input expression #4822

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec;
import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
* 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
* 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()}.
* <br />
*
Expand All @@ -45,6 +46,7 @@
* containing the required {@link org.bson.codecs.Codec codec} via {@link #withCodecRegistry(CodecRegistry)}.
*
* @author Christoph Strobl
* @author Giacomo Baso
* @since 3.2
*/
public class BindableMongoExpression implements MongoExpression {
Expand Down Expand Up @@ -77,7 +79,9 @@ public BindableMongoExpression(String expression, @Nullable Object[] args) {
public BindableMongoExpression(String expression, @Nullable CodecRegistryProvider codecRegistryProvider,
@Nullable Object[] args) {

this.expressionString = expression;
Assert.notNull(expression, "Expression must not be null");

this.expressionString = expression.trim();
this.codecRegistryProvider = codecRegistryProvider;
this.args = args;
this.target = Lazy.of(this::parse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Giacomo Baso
*/
@ExtendWith(MongoTemplateExtension.class)
@EnableIfMongoServerVersion(isGreaterThanEqual = "4.4")
Expand Down Expand Up @@ -89,6 +90,21 @@ void usesMongoExpressionWithPlaceholdersAsIs() {
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
void mapsAggregationExpressionToDomainType() {

Expand Down