Skip to content

Commit 76d6c14

Browse files
committed
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.
1 parent c2bfe42 commit 76d6c14

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
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.
@@ -35,6 +35,7 @@
3535
* property references into document field names.
3636
*
3737
* @author Oliver Gierke
38+
* @author Mark Paluch
3839
* @since 1.3
3940
*/
4041
public class TypeBasedAggregationOperationContext implements AggregationOperationContext {
@@ -96,7 +97,7 @@ private FieldReference getReferenceFor(Field field) {
9697

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

102103
return new DirectFieldReference(new ExposedField(mappedField, true));

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 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.
@@ -17,6 +17,7 @@
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.*;
2122
import static org.springframework.data.mongodb.core.aggregation.Fields.*;
2223
import static org.springframework.data.mongodb.test.util.IsBsonObject.*;
@@ -175,6 +176,27 @@ public void rendersAggregationOptionsInTypedAggregationContextCorrectly() {
175176
assertThat(dbo.get("cursor"), is((Object) new BasicDBObject("foo", 1)));
176177
}
177178

179+
/**
180+
* @see DATAMONGO-1586
181+
*/
182+
@Test
183+
public void rendersFieldAliasingProjectionCorrectly() {
184+
185+
AggregationOperationContext context = getContext(FooPerson.class);
186+
TypedAggregation<FooPerson> agg = newAggregation(FooPerson.class,
187+
project() //
188+
.and("name").as("person_name") //
189+
.and("age.value").as("age"));
190+
191+
DBObject dbo = agg.toDbObject("person", context);
192+
193+
DBObject projection = getPipelineElementFromAggregationAt(dbo, 0);
194+
assertThat(getAsDBObject(projection, "$project"),
195+
isBsonObject() //
196+
.containing("person_name", "$name") //
197+
.containing("age", "$age.value"));
198+
}
199+
178200
/**
179201
* @see DATAMONGO-1133
180202
*/

0 commit comments

Comments
 (0)