Skip to content

Commit 8df9d30

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 90ae6d1 commit 8df9d30

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

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

Lines changed: 5 additions & 4 deletions
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.
@@ -35,6 +35,7 @@
3535
*
3636
* @author Oliver Gierke
3737
* @author Christoph Strobl
38+
* @author Mark Paluch
3839
* @since 1.3
3940
*/
4041
public class TypeBasedAggregationOperationContext implements AggregationOperationContext {
@@ -94,9 +95,9 @@ public FieldReference getReference(String name) {
9495

9596
private FieldReference getReferenceFor(Field field) {
9697

97-
PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext
98-
.getPersistentPropertyPath(field.getTarget(), type);
99-
Field mappedField = field(propertyPath.getLeafProperty().getName(),
98+
PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(
99+
field.getTarget(), type);
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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.DocumentTestUtils.*;
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.*;
@@ -191,6 +192,27 @@ public void rendersSortOfProjectedFieldCorrectly() {
191192
assertThat(definition.get("counter"), is(equalTo((Object) 1)));
192193
}
193194

195+
/**
196+
* @see DATAMONGO-1586
197+
*/
198+
@Test
199+
public void rendersFieldAliasingProjectionCorrectly() {
200+
201+
AggregationOperationContext context = getContext(FooPerson.class);
202+
TypedAggregation<FooPerson> agg = newAggregation(FooPerson.class,
203+
project() //
204+
.and("name").as("person_name") //
205+
.and("age.value").as("age"));
206+
207+
Document dbo = agg.toDocument("person", context);
208+
209+
Document projection = getPipelineElementFromAggregationAt(dbo, 0);
210+
assertThat(getAsDocument(projection, "$project"),
211+
isBsonObject() //
212+
.containing("person_name", "$name") //
213+
.containing("age", "$age.value"));
214+
}
215+
194216
/**
195217
* @see DATAMONGO-1133
196218
*/
@@ -328,7 +350,9 @@ public void rendersAggregationConditionalInTypedAggregationContextCorrectly() {
328350
assertThat(getValue(age, "$cond"), isBsonObject().containing("else", "$age"));
329351
}
330352

331-
/**.AggregationUnitTests
353+
/**
354+
* .AggregationUnitTests
355+
*
332356
* @see DATAMONGO-861, DATAMONGO-1542
333357
*/
334358
@Test

0 commit comments

Comments
 (0)