Skip to content

DATAMONGO-2314 - Fix query by example on nested properties. #771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2314-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2314-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2314-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2314-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,19 @@ protected Document getMappedKeyword(Field property, Keyword keyword) {
Object convertedValue = needsAssociationConversion ? convertAssociation(value, property)
: getMappedValue(property.with(keyword.getKey()), value);

if(keyword.isSample() && convertedValue instanceof Document) {
return (Document) convertedValue;
}

return new Document(keyword.key, convertedValue);
}

/**
* Returns the mapped value for the given source object assuming it's a value for the given
* {@link MongoPersistentProperty}.
*
* @param documentField the key the value will be bound to eventually
* @param value the source object to be mapped
* @param property the property the value is a value for
* @param newKey the key the value will be bound to eventually
* @return
*/
@Nullable
Expand Down Expand Up @@ -448,6 +451,10 @@ protected boolean isAssociationConversionNecessary(Field documentField, @Nullabl
@SuppressWarnings("unchecked")
protected Object convertSimpleOrDocument(Object source, @Nullable MongoPersistentEntity<?> entity) {

if(source instanceof Example) {
return exampleMapper.getMappedExample((Example)source, entity);
}

if (source instanceof List) {
return delegateConvertToMongoType(source, entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,6 @@ public Criteria elemMatch(Criteria c) {
public Criteria alike(Example<?> sample) {

criteria.put("$example", sample);
this.criteriaChain.add(this);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
package org.springframework.data.mongodb.core;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;

import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.net.UnknownHostException;
import java.util.List;

import org.junit.Before;
Expand Down Expand Up @@ -50,7 +48,7 @@ public class QueryByExampleTests {
Person p1, p2, p3;

@Before
public void setUp() throws UnknownHostException {
public void setUp() {

operations = new MongoTemplate(new MongoClient(), "query-by-example");
operations.remove(new Query(), Person.class);
Expand Down Expand Up @@ -82,8 +80,7 @@ public void findByExampleShouldWorkForSimpleProperty() {
Query query = new Query(new Criteria().alike(Example.of(sample)));
List<Person> result = operations.find(query, Person.class);

assertThat(result, hasSize(2));
assertThat(result, hasItems(p1, p3));
assertThat(result).containsExactlyInAnyOrder(p1, p3);
}

@Test // DATAMONGO-1245
Expand All @@ -96,8 +93,7 @@ public void findByExampleShouldWorkForMultipleProperties() {
Query query = new Query(new Criteria().alike(Example.of(sample)));
List<Person> result = operations.find(query, Person.class);

assertThat(result, hasSize(1));
assertThat(result, hasItem(p3));
assertThat(result).containsExactly(p3);
}

@Test // DATAMONGO-1245
Expand All @@ -112,8 +108,7 @@ public void findByExampleShouldWorkForIdProperty() {
Query query = new Query(new Criteria().alike(Example.of(sample)));
List<Person> result = operations.find(query, Person.class);

assertThat(result, hasSize(1));
assertThat(result, hasItem(p4));
assertThat(result).containsExactly(p4);
}

@Test // DATAMONGO-1245
Expand All @@ -126,7 +121,7 @@ public void findByExampleShouldReturnEmptyListIfNotMatching() {
Query query = new Query(new Criteria().alike(Example.of(sample)));
List<Person> result = operations.find(query, Person.class);

assertThat(result, is(empty()));
assertThat(result).isEmpty();
}

@Test // DATAMONGO-1245
Expand All @@ -137,8 +132,7 @@ public void findByExampleShouldReturnEverythingWhenSampleIsEmpty() {
Query query = new Query(new Criteria().alike(Example.of(sample)));
List<Person> result = operations.find(query, Person.class);

assertThat(result, hasSize(3));
assertThat(result, hasItems(p1, p2, p3));
assertThat(result).containsExactlyInAnyOrder(p1, p2, p3);
}

@Test // DATAMONGO-1245
Expand All @@ -150,7 +144,7 @@ public void findByExampleWithCriteria() {
Query query = new Query(new Criteria().alike(Example.of(sample)).and("firstname").regex("^ary*"));

List<Person> result = operations.find(query, Person.class);
assertThat(result.size(), is(1));
assertThat(result).hasSize(1);
}

@Test // DATAMONGO-1459
Expand All @@ -163,8 +157,7 @@ public void findsExampleUsingAnyMatch() {
Query query = Query.query(Criteria.byExample(Example.of(probe, ExampleMatcher.matchingAny())));
List<Person> result = operations.find(query, Person.class);

assertThat(result, hasSize(2));
assertThat(result, hasItems(p1, p2));
assertThat(result).containsExactlyInAnyOrder(p1, p2);
}

@Test // DATAMONGO-1768
Expand All @@ -176,7 +169,7 @@ public void typedExampleMatchesNothingIfTypesDoNotMatch() {
Query query = new Query(new Criteria().alike(Example.of(probe)));
List<Person> result = operations.find(query, Person.class);

assertThat(result, hasSize(0));
assertThat(result).isEmpty();
}

@Test // DATAMONGO-1768
Expand All @@ -189,8 +182,7 @@ public void exampleIgnoringClassTypeKeyMatchesCorrectly() {
new Criteria().alike(Example.of(probe, ExampleMatcher.matching().withIgnorePaths("_class"))));
List<Person> result = operations.find(query, Person.class);

assertThat(result, hasSize(2));
assertThat(result, hasItems(p1, p3));
assertThat(result).containsExactlyInAnyOrder(p1, p3);
}

@Test // DATAMONGO-1768
Expand All @@ -202,8 +194,28 @@ public void untypedExampleMatchesCorrectly() {
Query query = new Query(new Criteria().alike(Example.of(probe, UntypedExampleMatcher.matching())));
List<Person> result = operations.find(query, Person.class);

assertThat(result, hasSize(2));
assertThat(result, hasItems(p1, p3));
assertThat(result).containsExactlyInAnyOrder(p1, p3);
}

@Test // DATAMONGO-2314
public void alikeShouldWorkOnNestedProperties() {

PersonWrapper source1 = new PersonWrapper();
source1.id = "with-child-doc-1";
source1.child = p1;

PersonWrapper source2 = new PersonWrapper();
source2.id = "with-child-doc-2";
source2.child = p2;

operations.save(source1);
operations.save(source2);

Query query = new Query(
new Criteria("child").alike(Example.of(p1, ExampleMatcher.matching().withIgnorePaths("_class"))));
List<PersonWrapper> result = operations.find(query, PersonWrapper.class);

assertThat(result).containsExactly(source1);
}

@Document("dramatis-personae")
Expand All @@ -223,4 +235,13 @@ static class NotAPersonButStillMatchingFields {
String firstname, middlename;
@Field("last_name") String lastname;
}

@Document("dramatis-personae")
@EqualsAndHashCode
@ToString
static class PersonWrapper {

@Id String id;
Person child;
}
}