From 116dda63c274b86d0145c5fc69f359ee89da526a Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 23 Aug 2016 16:38:54 +0200 Subject: [PATCH] DATAMONGO-1406 - Propagate PersistentEntity when mapping query criteria for nested keywords. We now propagate the PersistentEntity when mapping nested keywords so that the criteria mapping chain for nested keywords and properties has now access to the PersistentEntity and can use configured field names. Previously the plain property names have been used as field names and potential customizations via @Field have been ignored. Original Pull Request: #384 --- .../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 f43925447..d80423446 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 @@ import com.mongodb.DBRef; * @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 @@ public class QueryMapper { } 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 bc6b8272a..585ed114b 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.Test; 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.mapping.TextScore; 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 @@ import com.mongodb.QueryBuilder; * @author Patryk Wasik * @author Thomas Darimont * @author Christoph Strobl + * @author Mark Paluch */ @RunWith(MockitoJUnitRunner.class) public class QueryMapperUnitTests { @@ -595,6 +598,28 @@ public class QueryMapperUnitTests { 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 class QueryMapperUnitTests { } /** - * <<<<<<< HEAD - * + * * @see DATAMONGO-1269 */ @Test @@ -859,10 +883,15 @@ public class QueryMapperUnitTests { 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 {