Browse Source

Fix field inclusion in aggregation project operation.

Closes #3898
Original pull request: #3904.
3.2.x
Christoph Strobl 4 years ago committed by Mark Paluch
parent
commit
ba2b65cfd5
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 5
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
  2. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java
  3. 23
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationUnitTests.java

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

@ -1803,8 +1803,9 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
Document projections = new Document(); Document projections = new Document();
Fields fields = context.getFields(type); Fields fields = context.getFields(type);
fields.forEach(it -> projections.append(it.getName(), 1));
return context.getMappedObject(projections, type); fields.forEach(it -> projections.append(it.getTarget(), 1));
return projections;
} }
} }

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

@ -122,13 +122,13 @@ public class TypeBasedAggregationOperationContext implements AggregationOperatio
return AggregationOperationContext.super.getFields(type); return AggregationOperationContext.super.getFields(type);
} }
List<String> fields = new ArrayList<>(); List<Field> fields = new ArrayList<>();
for (MongoPersistentProperty property : entity) { for (MongoPersistentProperty property : entity) {
fields.add(property.getName()); fields.add(Fields.field(property.getName(), property.getFieldName()));
} }
return Fields.fields(fields.toArray(new String[0])); return Fields.from(fields.toArray(new Field[0]));
} }
/* /*

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

@ -27,6 +27,7 @@ import java.util.List;
import org.bson.Document; import org.bson.Document;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond; import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond;
import org.springframework.data.mongodb.core.aggregation.ProjectionOperationUnitTests.BookWithFieldAnnotation; import org.springframework.data.mongodb.core.aggregation.ProjectionOperationUnitTests.BookWithFieldAnnotation;
@ -598,9 +599,31 @@ public class AggregationUnitTests {
assertThat(extractPipelineElement(target, 1, "$project")).isEqualTo(Document.parse(" { \"_id\" : \"$_id\" }")); assertThat(extractPipelineElement(target, 1, "$project")).isEqualTo(Document.parse(" { \"_id\" : \"$_id\" }"));
} }
@Test // GH-3898
void shouldNotConvertIncludeExcludeValuesForProjectOperation() {
MongoMappingContext mappingContext = new MongoMappingContext();
RelaxedTypeBasedAggregationOperationContext context = new RelaxedTypeBasedAggregationOperationContext(WithRetypedIdField.class, mappingContext,
new QueryMapper(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext)));
Document document = project(WithRetypedIdField.class).toDocument(context);
assertThat(document).isEqualTo(new Document("$project", new Document("_id", 1).append("renamed-field", 1)));
}
private Document extractPipelineElement(Document agg, int index, String operation) { private Document extractPipelineElement(Document agg, int index, String operation) {
List<Document> pipeline = (List<Document>) agg.get("pipeline"); List<Document> pipeline = (List<Document>) agg.get("pipeline");
return (Document) pipeline.get(index).get(operation); return (Document) pipeline.get(index).get(operation);
} }
public class WithRetypedIdField {
@Id
@org.springframework.data.mongodb.core.mapping.Field
private String id;
@org.springframework.data.mongodb.core.mapping.Field("renamed-field")
private String foo;
}
} }

Loading…
Cancel
Save