Skip to content

Commit 5305c95

Browse files
Favor pipelineStages over toDocument in AggregationOperation.
This commit makes sure to use available pipeline stages to figure out the operation.
1 parent c02968d commit 5305c95

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020

2121
import org.bson.Document;
22+
import org.springframework.util.CollectionUtils;
2223

2324
/**
2425
* Represents one single operation in an aggregation pipeline.
@@ -63,6 +64,6 @@ default List<Document> toPipelineStages(AggregationOperationContext context) {
6364
* @since 3.0.2
6465
*/
6566
default String getOperator() {
66-
return toDocument(Aggregation.DEFAULT_CONTEXT).keySet().iterator().next();
67+
return CollectionUtils.lastElement(toPipelineStages(Aggregation.DEFAULT_CONTEXT)).keySet().iterator().next();
6768
}
6869
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.List;
21+
22+
import org.bson.Document;
23+
import org.junit.jupiter.api.Test;
24+
25+
/**
26+
* @author Christoph Strobl
27+
*/
28+
class AggregationOperationUnitTests {
29+
30+
@Test // GH-4306
31+
void getOperatorShouldFavorToPipelineStages() {
32+
33+
AggregationOperation op = new AggregationOperation() {
34+
35+
@Override
36+
public Document toDocument(AggregationOperationContext context) {
37+
return new Document();
38+
}
39+
40+
@Override
41+
public List<Document> toPipelineStages(AggregationOperationContext context) {
42+
return List.of(new Document("$spring", "data"));
43+
}
44+
};
45+
46+
assertThat(op.getOperator()).isEqualTo("$spring");
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import java.util.List;
19+
20+
import org.bson.Document;
21+
import org.junit.jupiter.api.Test;
22+
23+
/**
24+
* @author Christoph Strobl
25+
*/
26+
class AggregationPipelineUnitTests {
27+
28+
@Test // Gh-4306
29+
void verifyMustNotFailIfOnlyPipelineStagesUsed() {
30+
AggregationPipeline.of(new OverridesPipelineStagesAndThrowsErrorOnToDocument()).verify();
31+
}
32+
33+
static class OverridesPipelineStagesAndThrowsErrorOnToDocument implements AggregationOperation {
34+
35+
@Override
36+
public Document toDocument(AggregationOperationContext context) {
37+
throw new IllegalStateException("oh no");
38+
}
39+
40+
@Override
41+
public List<Document> toPipelineStages(AggregationOperationContext context) {
42+
return List.of(Aggregation.project("data").toDocument(context));
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)