Skip to content

Commit

Permalink
Retain regex options from the parsed JsonToken.
Browse files Browse the repository at this point in the history
We now retain expression options when resolving bind values from the original BsonRegularExpression.

Closes: #4806
Original Pull Request: #4807
  • Loading branch information
mp911de authored and christophstrobl committed Oct 15, 2024
1 parent 6e85051 commit 60a3461
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ private BindableValue bindableValueFor(JsonToken token) {

if (isRegularExpression) {

bindableValue.setValue(new BsonRegularExpression(computedValue));
BsonRegularExpression originalExpression = token.getValue(BsonRegularExpression.class);

bindableValue.setValue(new BsonRegularExpression(computedValue, originalExpression.getOptions()));
bindableValue.setType(BsonType.REGULAR_EXPRESSION);
} else {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

import org.bson.BsonBinary;
import org.bson.BsonBinarySubType;
import org.bson.BsonRegularExpression;
import org.bson.Document;
import org.bson.codecs.DecoderContext;
import org.junit.jupiter.api.Test;

import org.springframework.data.expression.ValueExpressionParser;
import org.springframework.data.mapping.model.ValueExpressionEvaluator;
import org.springframework.data.spel.EvaluationContextProvider;
import org.springframework.data.spel.ExpressionDependencies;
import org.springframework.expression.EvaluationContext;
Expand Down Expand Up @@ -84,6 +84,26 @@ void bindQuotedIntegerValue() {
assertThat(target).isEqualTo(new Document("lastname", "100"));
}

@Test // GH-4806
void regexConsidersOptions() {

Document target = parse("{ 'c': /^true$/i }");

BsonRegularExpression pattern = target.get("c", BsonRegularExpression.class);
assertThat(pattern.getPattern()).isEqualTo("^true$");
assertThat(pattern.getOptions()).isEqualTo("i");
}

@Test // GH-4806
void regexConsidersBindValueWithOptions() {

Document target = parse("{ 'c': /^?0$/i }", "foo");

BsonRegularExpression pattern = target.get("c", BsonRegularExpression.class);
assertThat(pattern.getPattern()).isEqualTo("^foo$");
assertThat(pattern.getOptions()).isEqualTo("i");
}

@Test
void bindValueToRegex() {

Expand Down

0 comments on commit 60a3461

Please sign in to comment.