Browse Source

DATAMONGO-1586 - Consider field name in TypeBasedAggregationOperationContext.getReferenceFor(…).

We now consider the provided field name (alias) in mapped fields with which it is exposed. The field name applies to the exposed field after property path resolution in TypeBasedAggregationOperationContext. Previously, the field reference used the property name which caused fields to be considered non-aliased, so aggregation projection operations dropped the alias and exposed the field with its leaf property name.

Original Pull Request: #434
pull/410/merge
Mark Paluch 9 years ago committed by Christoph Strobl
parent
commit
6c6ac6da5b
  1. 5
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java
  2. 23
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java

5
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2017 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.
@ -34,6 +34,7 @@ import com.mongodb.DBObject; @@ -34,6 +34,7 @@ import com.mongodb.DBObject;
* property references into document field names.
*
* @author Oliver Gierke
* @author Mark Paluch
* @since 1.3
*/
public class TypeBasedAggregationOperationContext implements AggregationOperationContext {
@ -95,7 +96,7 @@ public class TypeBasedAggregationOperationContext implements AggregationOperatio @@ -95,7 +96,7 @@ public class TypeBasedAggregationOperationContext implements AggregationOperatio
PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(
field.getTarget(), type);
Field mappedField = field(propertyPath.getLeafProperty().getName(),
Field mappedField = field(field.getName(),
propertyPath.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE));
return new FieldReference(new ExposedField(mappedField, true));

23
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java

@ -17,7 +17,9 @@ package org.springframework.data.mongodb.core.aggregation; @@ -17,7 +17,9 @@ package org.springframework.data.mongodb.core.aggregation;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.mongodb.core.DBObjectTestUtils.*;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.test.util.IsBsonObject.*;
import java.util.Arrays;
import java.util.List;
@ -188,6 +190,27 @@ public class TypeBasedAggregationOperationContextUnitTests { @@ -188,6 +190,27 @@ public class TypeBasedAggregationOperationContextUnitTests {
assertThat(definition.get("counter"), is(equalTo((Object) 1)));
}
/**
* @see DATAMONGO-1586
*/
@Test
public void rendersFieldAliasingProjectionCorrectly() {
AggregationOperationContext context = getContext(FooPerson.class);
TypedAggregation<FooPerson> agg = newAggregation(FooPerson.class,
project() //
.and("name").as("person_name") //
.and("age.value").as("age"));
DBObject dbo = agg.toDbObject("person", context);
DBObject projection = getPipelineElementFromAggregationAt(dbo, 0);
assertThat(getAsDBObject(projection, "$project"),
isBsonObject() //
.containing("person_name", "$name") //
.containing("age", "$age.value"));
}
/**
* @see DATAMONGO-1133
*/

Loading…
Cancel
Save