Skip to content

Commit 60a3461

Browse files
mp911dechristophstrobl
authored andcommitted
Retain regex options from the parsed JsonToken.
We now retain expression options when resolving bind values from the original BsonRegularExpression. Closes: #4806 Original Pull Request: #4807
1 parent 6e85051 commit 60a3461

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/json/ParameterBindingJsonReader.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ private BindableValue bindableValueFor(JsonToken token) {
461461

462462
if (isRegularExpression) {
463463

464-
bindableValue.setValue(new BsonRegularExpression(computedValue));
464+
BsonRegularExpression originalExpression = token.getValue(BsonRegularExpression.class);
465+
466+
bindableValue.setValue(new BsonRegularExpression(computedValue, originalExpression.getOptions()));
465467
bindableValue.setType(BsonType.REGULAR_EXPRESSION);
466468
} else {
467469

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/util/json/ParameterBindingJsonReaderUnitTests.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626

2727
import org.bson.BsonBinary;
2828
import org.bson.BsonBinarySubType;
29+
import org.bson.BsonRegularExpression;
2930
import org.bson.Document;
3031
import org.bson.codecs.DecoderContext;
3132
import org.junit.jupiter.api.Test;
3233

3334
import org.springframework.data.expression.ValueExpressionParser;
34-
import org.springframework.data.mapping.model.ValueExpressionEvaluator;
3535
import org.springframework.data.spel.EvaluationContextProvider;
3636
import org.springframework.data.spel.ExpressionDependencies;
3737
import org.springframework.expression.EvaluationContext;
@@ -84,6 +84,26 @@ void bindQuotedIntegerValue() {
8484
assertThat(target).isEqualTo(new Document("lastname", "100"));
8585
}
8686

87+
@Test // GH-4806
88+
void regexConsidersOptions() {
89+
90+
Document target = parse("{ 'c': /^true$/i }");
91+
92+
BsonRegularExpression pattern = target.get("c", BsonRegularExpression.class);
93+
assertThat(pattern.getPattern()).isEqualTo("^true$");
94+
assertThat(pattern.getOptions()).isEqualTo("i");
95+
}
96+
97+
@Test // GH-4806
98+
void regexConsidersBindValueWithOptions() {
99+
100+
Document target = parse("{ 'c': /^?0$/i }", "foo");
101+
102+
BsonRegularExpression pattern = target.get("c", BsonRegularExpression.class);
103+
assertThat(pattern.getPattern()).isEqualTo("^foo$");
104+
assertThat(pattern.getOptions()).isEqualTo("i");
105+
}
106+
87107
@Test
88108
void bindValueToRegex() {
89109

0 commit comments

Comments
 (0)