Skip to content

DATAMONGO-2200 - Derive fields for aggregation $project stage from a given type. #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2200-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2200-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2200-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2200-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ public static ProjectionOperation project(Fields fields) {
return new ProjectionOperation(fields);
}

/**
* Creates a new {@link ProjectionOperation} including all top level fields of the given given {@link Class}.
*
* @param type must not be {@literal null}.
* @return new instance of {@link ProjectionOperation}.
* @since 2.2
*/
public static ProjectionOperation project(Class<?> type) {

Assert.notNull(type, "Type must not be null!");
return new ProjectionOperation(type);
}

/**
* Factory method to create a new {@link UnwindOperation} for the field with the given name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@
*/
package org.springframework.data.mongodb.core.aggregation;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.bson.Document;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

/**
* The context for an {@link AggregationOperation}.
Expand Down Expand Up @@ -66,4 +75,32 @@ default Document getMappedObject(Document document) {
* @return
*/
FieldReference getReference(String name);

/**
* Returns the {@link Fields} exposed by the type. May be a {@literal class} or an {@literal interface}.
*
* @param type must not be {@literal null}.
* @return never {@literal null}.
* @since 2.2
*/
default Fields getFields(Class<?> type) {

Assert.notNull(type, "Type must not be null!");

List<String> fields = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
.filter(it -> { // object and default methods
Method method = it.getReadMethod();
if (method == null) {
return false;
}
if (ReflectionUtils.isObjectMethod(method)) {
return false;
}
return !method.isDefault();
}) //
.map(PropertyDescriptor::getName) //
.collect(Collectors.toList());

return Fields.fields(fields.toArray(new String[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public FieldReference getReference(String name) {
return getReference(null, name);
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getFields(java.lang.Class)
*/
@Override
public Fields getFields(Class<?> type) {
return rootContext.getFields(type);
}

/**
* Returns a {@link FieldReference} to the given {@link Field} with the given {@code name}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ public FieldReference getReference(Field field) {
public FieldReference getReference(String name) {
return new ExpressionFieldReference(delegate.getReference(name));
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getFields(java.lang.Class)
*/
@Override
public Fields getFields(Class<?> type) {
return delegate.getFields(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public FieldReference getReference(String name) {
return delegate.getReference(name);
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getFields(java.lang.Class)
*/
@Override
public Fields getFields(Class<?> type) {
return delegate.getFields(type);
}

@SuppressWarnings("unchecked")
private Document doPrefix(Document source) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

/**
* Encapsulates the aggregation framework {@code $project}-operation.
Expand Down Expand Up @@ -73,6 +74,16 @@ public ProjectionOperation(Fields fields) {
this(NONE, ProjectionOperationBuilder.FieldProjection.from(fields));
}

/**
* Creates a new {@link ProjectionOperation} including all top level fields of the given {@link Class type}.
*
* @param type must not be {@literal null}.
* @since 2.2
*/
public ProjectionOperation(Class<?> type) {
this(NONE, Collections.singletonList(new TypeProjection(type)));
}

/**
* Copy constructor to allow building up {@link ProjectionOperation} instances from already existing
* {@link Projection}s.
Expand Down Expand Up @@ -1700,4 +1711,36 @@ public Document toDocument(AggregationOperationContext context) {
return new Document(field.getName(), expression.toDocument(context));
}
}

/**
* A {@link Projection} including all top level fields of the given target type mapped to include potentially
* deviating field names.
*
* @since 2.2
* @author Christoph Strobl
*/
static class TypeProjection extends Projection {

private final Class<?> type;

TypeProjection(Class<?> type) {

super(Fields.field(type.getSimpleName()));
this.type = type;
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#toDocument(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
*/
@Override
public Document toDocument(AggregationOperationContext context) {

Document projections = new Document();

Fields fields = context.getFields(type);
fields.asList().forEach(it -> projections.append(it.getName(), 1));
return context.getMappedObject(projections, type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

import static org.springframework.data.mongodb.core.aggregation.Fields.*;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.mapping.SimplePropertyHandler;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
Expand Down Expand Up @@ -101,6 +104,23 @@ public FieldReference getReference(String name) {
return getReferenceFor(field(name));
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getFields(java.lang.Class)
*/
@Override
public Fields getFields(Class<?> type) {

MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(type);
if (entity == null) {
return AggregationOperationContext.super.getFields(type);
}

List<String> fields = new ArrayList<>();
entity.doWithProperties((SimplePropertyHandler) it -> fields.add(it.getName()));
return Fields.fields(fields.toArray(new String[fields.size()]));
}

private FieldReference getReferenceFor(Field field) {

PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation.ProjectionOperationBuilder;
import org.springframework.data.mongodb.core.aggregation.StringOperators.Concat;
import org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

/**
* Unit tests for {@link ProjectionOperation}.
Expand All @@ -61,7 +67,7 @@ public class ProjectionOperationUnitTests {

@Test(expected = IllegalArgumentException.class) // DATAMONGO-586
public void rejectsNullFields() {
new ProjectionOperation(null);
new ProjectionOperation((Fields) null);
}

@Test // DATAMONGO-586
Expand Down Expand Up @@ -2099,6 +2105,60 @@ public void shouldRenderDateFromStringWithFormat() {
"{ $project : { newDate: { $dateFromString: { dateString : \"2017-02-08T12:10:40.787\", format : \"dd/mm/yyyy\" } } } }"));
}

@Test // DATAMONGO-2200
public void typeProjectionShouldIncludeTopLevelFieldsOfType() {

ProjectionOperation operation = Aggregation.project(Book.class);

Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);

assertThat(projectClause) //
.hasSize(2) //
.containsEntry("title", 1) //
.containsEntry("author", 1);
}

@Test // DATAMONGO-2200
public void typeProjectionShouldMapFieldNames() {

MongoMappingContext mappingContext = new MongoMappingContext();
MongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);

Document document = Aggregation.project(BookRenamed.class)
.toDocument(new TypeBasedAggregationOperationContext(Book.class, mappingContext, new QueryMapper(converter)));
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);

assertThat(projectClause) //
.hasSize(2) //
.containsEntry("ti_tl_e", 1) //
.containsEntry("author", 1);
}

@Test // DATAMONGO-2200
public void typeProjectionShouldIncludeInterfaceProjectionValues() {

ProjectionOperation operation = Aggregation.project(ProjectionInterface.class);

Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);

assertThat(projectClause) //
.hasSize(1) //
.containsEntry("title", 1);
}

@Test // DATAMONGO-2200
public void typeProjectionShouldBeEmptyIfNoPropertiesFound() {

ProjectionOperation operation = Aggregation.project(EmptyType.class);

Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT);

assertThat(projectClause).isEmpty();
}

private static Document exctractOperation(String field, Document fromProjectClause) {
return (Document) fromProjectClause.get(field);
}
Expand All @@ -2109,11 +2169,25 @@ static class Book {
Author author;
}

@Data
static class BookRenamed {
@Field("ti_tl_e") String title;
Author author;
}

@Data
static class Author {
String first;
String last;
String middle;
}

interface ProjectionInterface {
String getTitle();
}

static class EmptyType {

}

}