Fix field inclusion in aggregation project operation.

Closes #3898
Original pull request: #3904.
This commit is contained in:
Christoph Strobl
2021-12-07 13:03:37 +01:00
committed by Mark Paluch
parent 2c9975e8db
commit ba2b65cfd5
3 changed files with 29 additions and 5 deletions

View File

@@ -1803,8 +1803,9 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
Document projections = new Document();
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;
}
}

View File

@@ -122,13 +122,13 @@ public class TypeBasedAggregationOperationContext implements AggregationOperatio
return AggregationOperationContext.super.getFields(type);
}
List<String> fields = new ArrayList<>();
List<Field> fields = new ArrayList<>();
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]));
}
/*

View File

@@ -27,6 +27,7 @@ import java.util.List;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond;
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\" }"));
}
@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) {
List<Document> pipeline = (List<Document>) agg.get("pipeline");
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;
}
}