Skip to content

Commit 07a224d

Browse files
committed
Polishing.
Simplify field creation considering simplified projection expressions. See #3917 Original pull request: #4328
1 parent aecca29 commit 07a224d

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

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

+7-17
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,13 @@ private FieldReference getReference(@Nullable Field field, String name) {
9696
return exposedField;
9797
}
9898

99-
if(rootContext instanceof RelaxedTypeBasedAggregationOperationContext) {
100-
return new DirectFieldReference(new ExposedField(new Field() {
101-
@Override
102-
public String getName() {
103-
return name;
104-
}
105-
106-
@Override
107-
public String getTarget() {
108-
return field != null ? field.getTarget() : name;
109-
}
110-
111-
@Override
112-
public boolean isAliased() {
113-
return true;
114-
}
115-
}, true));
99+
if (rootContext instanceof RelaxedTypeBasedAggregationOperationContext) {
100+
101+
if (field != null) {
102+
return new DirectFieldReference(new ExposedField(field, true));
103+
}
104+
105+
return new DirectFieldReference(new ExposedField(name, true));
116106
}
117107

118108
throw new IllegalArgumentException(String.format("Invalid reference '%s'", name));

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ void projectOnIdIsAlwaysValid() {
601601
new RelaxedTypeBasedAggregationOperationContext(BookWithFieldAnnotation.class, mappingContext,
602602
new QueryMapper(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext))));
603603

604-
assertThat(extractPipelineElement(target, 1, "$project")).isEqualTo(Document.parse(" { \"_id\" : \"$_id\" }"));
604+
assertThat(extractPipelineElement(target, 1, "$project")).isEqualTo(Document.parse(" { \"_id\" : 1 }"));
605605
}
606606

607607
@Test // GH-3898
@@ -630,7 +630,7 @@ void createsBasicAggregationOperationFromBson() {
630630
Document target = newAggregation(stage).toDocument("col-1", DEFAULT_CONTEXT);
631631
assertThat(extractPipelineElement(target, 0, "$project")).containsKey("name");
632632
}
633-
633+
634634
@Test // GH-3917
635635
void inheritedFieldsExposingContextShouldNotFailOnUnknownFieldReferenceForRelaxedRootContext() {
636636

@@ -648,7 +648,7 @@ void inheritedFieldsExposingContextShouldNotFailOnUnknownFieldReferenceForRelaxe
648648

649649
MongoMappingContext mappingContext = new MongoMappingContext();
650650
QueryMapper queryMapper = new QueryMapper(new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));
651-
651+
652652
List<Document> documents = newAggregation(City.class, aggregationOperations).toPipeline(new RelaxedTypeBasedAggregationOperationContext(City.class, mappingContext, queryMapper));
653653
assertThat(documents.get(2)).isEqualTo("{ $sort : { 'serial_number' : -1, 'label_name' : -1 } }");
654654
}

0 commit comments

Comments
 (0)