Skip to content

Commit 116dda6

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 4649872 commit 116dda6

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
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
@@ -58,6 +58,7 @@
5858
* @author Patryk Wasik
5959
* @author Thomas Darimont
6060
* @author Christoph Strobl
61+
* @author Mark Paluch
6162
*/
6263
public class QueryMapper {
6364

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

6869
private enum MetaMapping {
69-
FORCE, WHEN_PRESENT, IGNORE;
70+
FORCE, WHEN_PRESENT, IGNORE
7071
}
7172

7273
private final ConversionService conversionService;
@@ -316,7 +317,7 @@ protected Object getMappedValue(Field documentField, Object value) {
316317
}
317318

318319
if (isNestedKeyword(value)) {
319-
return getMappedKeyword(new Keyword((DBObject) value), null);
320+
return getMappedKeyword(new Keyword((DBObject) value), documentField.getPropertyEntity());
320321
}
321322

322323
if (isAssociationConversionNecessary(documentField, value)) {

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

Lines changed: 32 additions & 3 deletions
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.
@@ -35,6 +35,7 @@
3535
import org.junit.runner.RunWith;
3636
import org.mockito.Mock;
3737
import org.mockito.runners.MockitoJUnitRunner;
38+
3839
import org.springframework.data.annotation.Id;
3940
import org.springframework.data.domain.Sort;
4041
import org.springframework.data.domain.Sort.Direction;
@@ -54,6 +55,7 @@
5455
import org.springframework.data.mongodb.core.query.BasicQuery;
5556
import org.springframework.data.mongodb.core.query.Criteria;
5657
import org.springframework.data.mongodb.core.query.Query;
58+
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
5759

5860
import com.mongodb.BasicDBList;
5961
import com.mongodb.BasicDBObject;
@@ -68,6 +70,7 @@
6870
* @author Patryk Wasik
6971
* @author Thomas Darimont
7072
* @author Christoph Strobl
73+
* @author Mark Paluch
7174
*/
7275
@RunWith(MockitoJUnitRunner.class)
7376
public class QueryMapperUnitTests {
@@ -595,6 +598,28 @@ public void classInformationShouldNotBePresentInDBObjectUsedInFinderMethods() {
595598
assertThat(dbo.toString(), equalTo("{ \"embedded\" : { \"$in\" : [ { \"_id\" : \"1\"} , { \"_id\" : \"2\"}]}}"));
596599
}
597600

601+
/**
602+
* @see DATAMONGO-1406
603+
*/
604+
@Test
605+
public void shouldMapQueryForNestedCustomizedPropertiesUsingConfiguredFieldNames() {
606+
607+
EmbeddedClass embeddedClass = new EmbeddedClass();
608+
embeddedClass.customizedField = "hello";
609+
610+
Foo foo = new Foo();
611+
foo.listOfItems = Arrays.asList(embeddedClass);
612+
613+
Query query = new Query(Criteria.where("listOfItems") //
614+
.elemMatch(new Criteria(). //
615+
andOperator(Criteria.where("customizedField").is(embeddedClass.customizedField))));
616+
617+
DBObject dbo = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class));
618+
619+
assertThat(dbo, isBsonObject().containing("my_items.$elemMatch.$and",
620+
new BasicDbListBuilder().add(new BasicDBObject("fancy_custom_name", embeddedClass.customizedField)).get()));
621+
}
622+
598623
/**
599624
* @see DATAMONGO-647
600625
*/
@@ -792,8 +817,7 @@ public void intersectsShouldUseGeoJsonRepresentationCorrectly() {
792817
}
793818

794819
/**
795-
* <<<<<<< HEAD
796-
*
820+
*
797821
* @see DATAMONGO-1269
798822
*/
799823
@Test
@@ -859,10 +883,15 @@ public void exampleShouldBeMappedCorrectlyWhenContainingLegacyPoint() {
859883
public class Foo {
860884
@Id private ObjectId id;
861885
EmbeddedClass embedded;
886+
887+
@Field("my_items")
888+
List<EmbeddedClass> listOfItems;
862889
}
863890

864891
public class EmbeddedClass {
865892
public String id;
893+
894+
@Field("fancy_custom_name") public String customizedField;
866895
}
867896

868897
class IdWrapper {

0 commit comments

Comments
 (0)