From 593e54ff1275498318d4c90ff964f6217e007cc0 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 23 Aug 2016 16:39:55 +0200 Subject: [PATCH 1/2] DATAMONGO-1406 - Query mapper does not use @Field field name when querying nested fields in combination with nested keywords. Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-cross-store/pom.xml | 4 ++-- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb-log4j/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index dfb64f9ba3..306c5eadaf 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1406-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index ae0a5d6c8f..f6234d32b5 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1406-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1406-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 2d02722262..fc15609dba 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1406-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index ee5e3336db..f51f9859a5 100644 --- a/spring-data-mongodb-log4j/pom.xml +++ b/spring-data-mongodb-log4j/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1406-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 8072d3f665..636139ddc3 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 1.10.0.BUILD-SNAPSHOT + 1.10.0.DATAMONGO-1406-SNAPSHOT ../pom.xml From 8925c732e8e81e7219b4dd9ed6582fea4c99fe7a Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 23 Aug 2016 16:38:54 +0200 Subject: [PATCH 2/2] DATAMONGO-1406 - Propagate PersistentEntity when mapping query criteria for nested keywords. Previously, query mapping of nested keywords combined with nested properties did not pass the PersistentEntity. That resulted in the usage of property names as field names instead of using configuration details of @Field. The QueryMapper now propagates the PersistentEntity when mapping nested keywords. The criteria mapping chain for nested keywords and properties has now access to the PersistentEntity and can use configured field names. --- .../mongodb/core/convert/QueryMapper.java | 5 +-- .../core/convert/QueryMapperUnitTests.java | 35 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java index f439254475..d80423446b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java @@ -58,6 +58,7 @@ * @author Patryk Wasik * @author Thomas Darimont * @author Christoph Strobl + * @author Mark Paluch */ public class QueryMapper { @@ -66,7 +67,7 @@ public class QueryMapper { static final ClassTypeInformation NESTED_DOCUMENT = ClassTypeInformation.from(NestedDocument.class); private enum MetaMapping { - FORCE, WHEN_PRESENT, IGNORE; + FORCE, WHEN_PRESENT, IGNORE } private final ConversionService conversionService; @@ -316,7 +317,7 @@ protected Object getMappedValue(Field documentField, Object value) { } if (isNestedKeyword(value)) { - return getMappedKeyword(new Keyword((DBObject) value), null); + return getMappedKeyword(new Keyword((DBObject) value), documentField.getPropertyEntity()); } if (isAssociationConversionNecessary(documentField, value)) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java index bc6b8272ac..585ed114bd 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2015 the original author or authors. + * Copyright 2011-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; + import org.springframework.data.annotation.Id; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; @@ -54,6 +55,7 @@ import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.test.util.BasicDbListBuilder; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; @@ -68,6 +70,7 @@ * @author Patryk Wasik * @author Thomas Darimont * @author Christoph Strobl + * @author Mark Paluch */ @RunWith(MockitoJUnitRunner.class) public class QueryMapperUnitTests { @@ -595,6 +598,28 @@ public void classInformationShouldNotBePresentInDBObjectUsedInFinderMethods() { assertThat(dbo.toString(), equalTo("{ \"embedded\" : { \"$in\" : [ { \"_id\" : \"1\"} , { \"_id\" : \"2\"}]}}")); } + /** + * @see DATAMONGO-1406 + */ + @Test + public void shouldMapQueryForNestedCustomizedPropertiesUsingConfiguredFieldNames() { + + EmbeddedClass embeddedClass = new EmbeddedClass(); + embeddedClass.customizedField = "hello"; + + Foo foo = new Foo(); + foo.listOfItems = Arrays.asList(embeddedClass); + + Query query = new Query(Criteria.where("listOfItems") // + .elemMatch(new Criteria(). // + andOperator(Criteria.where("customizedField").is(embeddedClass.customizedField)))); + + DBObject dbo = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class)); + + assertThat(dbo, isBsonObject().containing("my_items.$elemMatch.$and", + new BasicDbListBuilder().add(new BasicDBObject("fancy_custom_name", embeddedClass.customizedField)).get())); + } + /** * @see DATAMONGO-647 */ @@ -792,8 +817,7 @@ public void intersectsShouldUseGeoJsonRepresentationCorrectly() { } /** - * <<<<<<< HEAD - * + * * @see DATAMONGO-1269 */ @Test @@ -859,10 +883,15 @@ public void exampleShouldBeMappedCorrectlyWhenContainingLegacyPoint() { public class Foo { @Id private ObjectId id; EmbeddedClass embedded; + + @Field("my_items") + List listOfItems; } public class EmbeddedClass { public String id; + + @Field("fancy_custom_name") public String customizedField; } class IdWrapper {