File tree 3 files changed +97
-1
lines changed
main/java/org/springframework/data/mongodb/core/aggregation
test/java/org/springframework/data/mongodb/core/aggregation
3 files changed +97
-1
lines changed Original file line number Diff line number Diff line change 19
19
import java .util .List ;
20
20
21
21
import org .bson .Document ;
22
+ import org .springframework .util .CollectionUtils ;
22
23
23
24
/**
24
25
* Represents one single operation in an aggregation pipeline.
@@ -63,6 +64,6 @@ default List<Document> toPipelineStages(AggregationOperationContext context) {
63
64
* @since 3.0.2
64
65
*/
65
66
default String getOperator () {
66
- return toDocument ( Aggregation .DEFAULT_CONTEXT ).keySet ().iterator ().next ();
67
+ return CollectionUtils . lastElement ( toPipelineStages ( Aggregation .DEFAULT_CONTEXT ) ).keySet ().iterator ().next ();
67
68
}
68
69
}
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments