Skip to content

Commit 0561b80

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 05e90fa commit 0561b80

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
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
@@ -456,7 +456,9 @@ private BindableValue bindableValueFor(JsonToken token) {
456456

457457
if (isRegularExpression) {
458458

459-
bindableValue.setValue(new BsonRegularExpression(computedValue));
459+
BsonRegularExpression originalExpression = token.getValue(BsonRegularExpression.class);
460+
461+
bindableValue.setValue(new BsonRegularExpression(computedValue, originalExpression.getOptions()));
460462
bindableValue.setType(BsonType.REGULAR_EXPRESSION);
461463
} else {
462464

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

+21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
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;
@@ -81,6 +82,26 @@ void bindQuotedIntegerValue() {
8182
assertThat(target).isEqualTo(new Document("lastname", "100"));
8283
}
8384

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

0 commit comments

Comments
 (0)