Skip to content

Commit 1f403b2

Browse files
author
jmarkerink
committed
fix: server errors and related unit tests
1 parent 3f3712a commit 1f403b2

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/Expression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,14 +1431,14 @@ Object apply(Object expressionValue, Document document) {
14311431

14321432
// Validate that 'branches' field exists
14331433
if (!switchDocument.containsKey("branches")) {
1434-
throw new MongoServerError(40061, "Missing 'branches' parameter to " + name());
1434+
throw new MongoServerError(40068, name() + " requires at least one branch");
14351435
}
14361436

14371437
// Validate unsupported parameters
14381438
List<String> supportedKeys = asList("branches", "default");
14391439
for (String key : switchDocument.keySet()) {
14401440
if (!supportedKeys.contains(key)) {
1441-
throw new MongoServerError(40067, "Unrecognized parameter to " + name() + ": " + key);
1441+
throw new MongoServerError(40067, name() + " found an unknown argument: " + key);
14421442
}
14431443
}
14441444

test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractAggregationTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2830,7 +2830,28 @@ void testAggregateWithSwitchMissingBranches() throws Exception {
28302830

28312831
assertThatExceptionOfType(MongoCommandException.class)
28322832
.isThrownBy(() -> collection.aggregate(pipeline).first())
2833-
.withMessageContaining("Missing 'branches' parameter to $switch");
2833+
.withMessageContaining("$switch requires at least one branch");
2834+
}
2835+
2836+
@Test
2837+
void testAggregateWithSwitchEmptyBranches() throws Exception {
2838+
collection.insertOne(json("_id: 1, value: 100"));
2839+
2840+
List<Document> pipeline = jsonList("""
2841+
$project: {
2842+
result: {
2843+
$switch: {
2844+
branches: [
2845+
],
2846+
default: 'none'
2847+
}
2848+
}
2849+
}
2850+
""");
2851+
2852+
assertThatExceptionOfType(MongoCommandException.class)
2853+
.isThrownBy(() -> collection.aggregate(pipeline).first())
2854+
.withMessageContaining("$switch requires at least one branch");
28342855
}
28352856

28362857
@Test
@@ -2855,6 +2876,28 @@ void testAggregateWithSwitchInvalidBranch() throws Exception {
28552876
.withMessageContaining("$switch requires each branch have a 'then' expression");
28562877
}
28572878

2879+
@Test
2880+
void testAggregateWithSwitchInvalidArgument() throws Exception {
2881+
collection.insertOne(json("_id: 1, value: 100"));
2882+
2883+
List<Document> pipeline = jsonList("""
2884+
$project: {
2885+
result: {
2886+
$switch: {
2887+
branches: [
2888+
{ case: { $eq: ['$value', 100] }, then: 'one hundred' }
2889+
],
2890+
default_value: 'none'
2891+
}
2892+
}
2893+
}
2894+
""");
2895+
2896+
assertThatExceptionOfType(MongoCommandException.class)
2897+
.isThrownBy(() -> collection.aggregate(pipeline).first())
2898+
.withMessageContaining("$switch found an unknown argument: default_value");
2899+
}
2900+
28582901
// https://github.com/bwaldvogel/mongo-java-server/issues/138
28592902
@Test
28602903
public void testAggregateWithGeoNear() throws Exception {

0 commit comments

Comments
 (0)