From 8df9d30d2e6c88f5615f554412f381624e8ba0f9 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 9 Jan 2017 15:40:51 +0100 Subject: [PATCH] =?UTF-8?q?DATAMONGO-1586=20-=20Consider=20field=20name=20?= =?UTF-8?q?in=20TypeBasedAggregationOperationContext.getReferenceFor(?= =?UTF-8?q?=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../TypeBasedAggregationOperationContext.java | 9 ++++--- ...dAggregationOperationContextUnitTests.java | 26 ++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java index 3911ab08f..cebc25626 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2016 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. @@ -35,6 +35,7 @@ import org.springframework.util.Assert; * * @author Oliver Gierke * @author Christoph Strobl + * @author Mark Paluch * @since 1.3 */ public class TypeBasedAggregationOperationContext implements AggregationOperationContext { @@ -94,9 +95,9 @@ public class TypeBasedAggregationOperationContext implements AggregationOperatio private FieldReference getReferenceFor(Field field) { - PersistentPropertyPath propertyPath = mappingContext - .getPersistentPropertyPath(field.getTarget(), type); - Field mappedField = field(propertyPath.getLeafProperty().getName(), + PersistentPropertyPath propertyPath = mappingContext.getPersistentPropertyPath( + field.getTarget(), type); + Field mappedField = field(field.getName(), propertyPath.toDotPath(MongoPersistentProperty.PropertyToFieldNameConverter.INSTANCE)); return new DirectFieldReference(new ExposedField(mappedField, true)); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java index a3984bdf9..9bb1f7545 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java @@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core.aggregation; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import static org.springframework.data.mongodb.core.DocumentTestUtils.*; import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; import static org.springframework.data.mongodb.core.aggregation.Fields.*; import static org.springframework.data.mongodb.test.util.IsBsonObject.*; @@ -191,6 +192,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 agg = newAggregation(FooPerson.class, + project() // + .and("name").as("person_name") // + .and("age.value").as("age")); + + Document dbo = agg.toDocument("person", context); + + Document projection = getPipelineElementFromAggregationAt(dbo, 0); + assertThat(getAsDocument(projection, "$project"), + isBsonObject() // + .containing("person_name", "$name") // + .containing("age", "$age.value")); + } + /** * @see DATAMONGO-1133 */ @@ -328,7 +350,9 @@ public class TypeBasedAggregationOperationContextUnitTests { assertThat(getValue(age, "$cond"), isBsonObject().containing("else", "$age")); } - /**.AggregationUnitTests + /** + * .AggregationUnitTests + * * @see DATAMONGO-861, DATAMONGO-1542 */ @Test