Skip to content

Commit da5c062

Browse files
christophstroblmp911de
authored andcommitted
Wrap Criteria is and regex comparison if necessary.
This commit wraps simple values and Patterns if to avoid creating invalid query objects. Original pull request: #4862 Closes #4850
1 parent 639e2c2 commit da5c062

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -945,8 +945,17 @@ protected Document getSingleCriteriaObject() {
945945
Document queryCriteria = new Document();
946946

947947
if (!NOT_SET.equals(isValue)) {
948-
queryCriteria.put(this.key, this.isValue);
949-
queryCriteria.putAll(document);
948+
if(document.isEmpty()) {
949+
queryCriteria.put(this.key, this.isValue);
950+
}
951+
else {
952+
if(isValue instanceof Pattern || isValue instanceof BsonRegularExpression) {
953+
document.put("$regex", isValue);
954+
} else {
955+
document.put("$eq", isValue);
956+
}
957+
queryCriteria.put(this.key, document);
958+
}
950959
} else {
951960
queryCriteria.put(this.key, document);
952961
}

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java

+40
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import java.util.Arrays;
2121
import java.util.Collection;
2222
import java.util.Collections;
23+
import java.util.regex.Pattern;
2324

25+
import org.bson.BsonRegularExpression;
2426
import org.bson.Document;
2527
import org.junit.Test;
2628
import org.springframework.data.geo.Point;
@@ -50,6 +52,44 @@ public void testSimpleCriteria() {
5052
assertThat(c.getCriteriaObject()).isEqualTo("{ \"name\" : \"Bubba\"}");
5153
}
5254

55+
@Test // GH-4850
56+
public void testCombiningSimpleCriteria() {
57+
58+
Document expected = Document.parse("{ name : { $eq : 123, $type : ['long'] } }");
59+
60+
Criteria c = Criteria.where("name") //
61+
.is(123) //
62+
.type(Type.INT_64);
63+
64+
assertThat(c.getCriteriaObject()).isEqualTo(expected);
65+
66+
c = Criteria.where("name") //
67+
.type(Type.INT_64)
68+
.is(123);
69+
70+
assertThat(c.getCriteriaObject()).isEqualTo(expected);
71+
}
72+
73+
@Test // GH-4850
74+
public void testCombiningBsonRegexCriteria() {
75+
76+
Criteria c = Criteria.where("name")
77+
.regex(new BsonRegularExpression("^spring$"))
78+
.type(Type.INT_64);
79+
80+
assertThat(c.getCriteriaObject()).isEqualTo(Document.parse("{ name : { $regex : RegExp('^spring$'), $type : ['long'] } }"));
81+
}
82+
83+
@Test // GH-4850
84+
public void testCombiningRegexCriteria() {
85+
86+
Criteria c = Criteria.where("name")
87+
.regex("^spring$")
88+
.type(Type.INT_64);
89+
90+
assertThat(c.getCriteriaObject()).hasEntrySatisfying("name.$regex", it -> assertThat(it).isInstanceOf(Pattern.class));
91+
}
92+
5393
@Test
5494
public void testNotEqualCriteria() {
5595
Criteria c = new Criteria("name").ne("Bubba");

0 commit comments

Comments
 (0)