Skip to content

Commit fc39306

Browse files
christophstroblmp911de
authored andcommitted
Fix Criteria chaining for Criteria.alike().
This commit fixes an issue where an Example probe would not be added to the criteria chain. Closes #3544 Original pull request: #3549.
1 parent 9c17ee0 commit fc39306

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,15 @@ public Criteria elemMatch(Criteria criteria) {
615615
*/
616616
public Criteria alike(Example<?> sample) {
617617

618-
criteria.put("$example", sample);
619-
return this;
618+
if (StringUtils.hasText(this.getKey())) {
619+
620+
criteria.put("$example", sample);
621+
return this;
622+
}
623+
624+
Criteria exampleCriteria = new Criteria();
625+
exampleCriteria.criteria.put("$example", sample);
626+
return registerCriteriaChainElement(exampleCriteria);
620627
}
621628

622629
/**

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,14 @@ public void findByExampleShouldReturnEverythingWhenSampleIsEmpty() {
134134
assertThat(result).containsExactlyInAnyOrder(p1, p2, p3);
135135
}
136136

137-
@Test // DATAMONGO-1245
137+
@Test // DATAMONGO-1245, GH-3544
138138
public void findByExampleWithCriteria() {
139139

140140
Person sample = new Person();
141141
sample.lastname = "stark";
142142

143-
Query query = new Query(new Criteria().alike(Example.of(sample)).and("firstname").regex("^ary*"));
144-
145-
List<Person> result = operations.find(query, Person.class);
146-
assertThat(result).hasSize(1);
143+
Query query = new Query(new Criteria().alike(Example.of(sample)).and("firstname").regex(".*n.*"));
144+
assertThat(operations.find(query, Person.class)).containsExactly(p1);
147145
}
148146

149147
@Test // DATAMONGO-1459

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,19 @@ public void exampleShouldBeMappedCorrectlyWhenContainingLegacyPoint() {
772772
assertThat(document.get("legacyPoint.y")).isEqualTo(20D);
773773
}
774774

775+
@Test // GH-3544
776+
public void exampleWithCombinedCriteriaShouldBeMappedCorrectly() {
777+
778+
Foo probe = new Foo();
779+
probe.embedded = new EmbeddedClass();
780+
probe.embedded.id = "conflux";
781+
782+
Query query = query(byExample(probe).and("listOfItems").exists(true));
783+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class));
784+
785+
assertThat(document).containsEntry("embedded\\._id", "conflux").containsEntry("my_items", new org.bson.Document("$exists", true));
786+
}
787+
775788
@Test // DATAMONGO-1988
776789
public void mapsStringObjectIdRepresentationToObjectIdWhenReferencingIdProperty() {
777790

0 commit comments

Comments
 (0)