Skip to content

Commit 6c6ac6d

Browse files
mp911dechristophstrobl
authored andcommitted
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
1 parent c1ac876 commit 6c6ac6d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2017 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.
@@ -34,6 +34,7 @@
3434
* property references into document field names.
3535
*
3636
* @author Oliver Gierke
37+
* @author Mark Paluch
3738
* @since 1.3
3839
*/
3940
public class TypeBasedAggregationOperationContext implements AggregationOperationContext {
@@ -95,7 +96,7 @@ private FieldReference getReferenceFor(Field field) {
9596

9697
PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(
9798
field.getTarget(), type);
98-
Field mappedField = field(propertyPath.getLeafProperty().getName(),
99+
Field mappedField = field(field.getName(),
99100
propertyPath.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE));
100101

101102
return new FieldReference(new ExposedField(mappedField, true));

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
20+
import static org.springframework.data.mongodb.core.DBObjectTestUtils.*;
2021
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
22+
import static org.springframework.data.mongodb.test.util.IsBsonObject.*;
2123

2224
import java.util.Arrays;
2325
import java.util.List;
@@ -188,6 +190,27 @@ public void rendersSortOfProjectedFieldCorrectly() {
188190
assertThat(definition.get("counter"), is(equalTo((Object) 1)));
189191
}
190192

193+
/**
194+
* @see DATAMONGO-1586
195+
*/
196+
@Test
197+
public void rendersFieldAliasingProjectionCorrectly() {
198+
199+
AggregationOperationContext context = getContext(FooPerson.class);
200+
TypedAggregation<FooPerson> agg = newAggregation(FooPerson.class,
201+
project() //
202+
.and("name").as("person_name") //
203+
.and("age.value").as("age"));
204+
205+
DBObject dbo = agg.toDbObject("person", context);
206+
207+
DBObject projection = getPipelineElementFromAggregationAt(dbo, 0);
208+
assertThat(getAsDBObject(projection, "$project"),
209+
isBsonObject() //
210+
.containing("person_name", "$name") //
211+
.containing("age", "$age.value"));
212+
}
213+
191214
/**
192215
* @see DATAMONGO-1133
193216
*/

0 commit comments

Comments
 (0)