Skip to content

Commit f7ab448

Browse files
mp911dechristophstrobl
authored andcommitted
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
1 parent 2026097 commit f7ab448

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @author Patryk Wasik
5858
* @author Thomas Darimont
5959
* @author Christoph Strobl
60+
* @author Mark Paluch
6061
*/
6162
public class QueryMapper {
6263

@@ -65,7 +66,7 @@ public class QueryMapper {
6566
static final ClassTypeInformation<?> NESTED_DOCUMENT = ClassTypeInformation.from(NestedDocument.class);
6667

6768
private enum MetaMapping {
68-
FORCE, WHEN_PRESENT, IGNORE;
69+
FORCE, WHEN_PRESENT, IGNORE
6970
}
7071

7172
private final ConversionService conversionService;
@@ -309,7 +310,7 @@ protected Object getMappedValue(Field documentField, Object value) {
309310
}
310311

311312
if (isNestedKeyword(value)) {
312-
return getMappedKeyword(new Keyword((DBObject) value), null);
313+
return getMappedKeyword(new Keyword((DBObject) value), documentField.getPropertyEntity());
313314
}
314315

315316
if (isAssociationConversionNecessary(documentField, value)) {

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,7 @@
5353
import org.springframework.data.mongodb.core.query.BasicQuery;
5454
import org.springframework.data.mongodb.core.query.Criteria;
5555
import org.springframework.data.mongodb.core.query.Query;
56+
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
5657

5758
import com.mongodb.BasicDBList;
5859
import com.mongodb.BasicDBObject;
@@ -67,6 +68,7 @@
6768
* @author Patryk Wasik
6869
* @author Thomas Darimont
6970
* @author Christoph Strobl
71+
* @author Mark Paluch
7072
*/
7173
@RunWith(MockitoJUnitRunner.class)
7274
public class QueryMapperUnitTests {
@@ -594,6 +596,28 @@ public void classInformationShouldNotBePresentInDBObjectUsedInFinderMethods() {
594596
assertThat(dbo.toString(), equalTo("{ \"embedded\" : { \"$in\" : [ { \"_id\" : \"1\"} , { \"_id\" : \"2\"}]}}"));
595597
}
596598

599+
/**
600+
* @see DATAMONGO-1406
601+
*/
602+
@Test
603+
public void shouldMapQueryForNestedCustomizedPropertiesUsingConfiguredFieldNames() {
604+
605+
EmbeddedClass embeddedClass = new EmbeddedClass();
606+
embeddedClass.customizedField = "hello";
607+
608+
Foo foo = new Foo();
609+
foo.listOfItems = Arrays.asList(embeddedClass);
610+
611+
Query query = new Query(Criteria.where("listOfItems") //
612+
.elemMatch(new Criteria(). //
613+
andOperator(Criteria.where("customizedField").is(embeddedClass.customizedField))));
614+
615+
DBObject dbo = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class));
616+
617+
assertThat(dbo, isBsonObject().containing("my_items.$elemMatch.$and",
618+
new BasicDbListBuilder().add(new BasicDBObject("fancy_custom_name", embeddedClass.customizedField)).get()));
619+
}
620+
597621
/**
598622
* @see DATAMONGO-647
599623
*/
@@ -822,10 +846,14 @@ public void mappingShouldRetainNumericPositionInList() {
822846
public class Foo {
823847
@Id private ObjectId id;
824848
EmbeddedClass embedded;
849+
850+
@Field("my_items") List<EmbeddedClass> listOfItems;
825851
}
826852

827853
public class EmbeddedClass {
828854
public String id;
855+
856+
@Field("fancy_custom_name") public String customizedField;
829857
}
830858

831859
class IdWrapper {

0 commit comments

Comments
 (0)