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
This commit is contained in:
Mark Paluch
2016-08-23 16:38:54 +02:00
committed by Christoph Strobl
parent 4649872394
commit 116dda63c2
2 changed files with 35 additions and 5 deletions

View File

@@ -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)) {

View File

@@ -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<EmbeddedClass> listOfItems;
}
public class EmbeddedClass {
public String id;
@Field("fancy_custom_name") public String customizedField;
}
class IdWrapper {