Skip to content

Commit 5e478bc

Browse files
committed
Fix return type of $split aggregation #214
This is relevant if the values are processed in other expressions.
1 parent 8effe55 commit 5e478bc

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,8 @@ Object apply(List<?> expressionValue, Document document) {
13791379
name() + " requires an expression that evaluates to a string as a second argument, found: " + describeType(delimiter));
13801380
}
13811381

1382-
return ((String) string).split(Pattern.quote((String) delimiter));
1382+
String[] values = ((String) string).split(Pattern.quote((String) delimiter), -1);
1383+
return List.of(values);
13831384
}
13841385
},
13851386

core/src/main/java/de/bwaldvogel/mongo/wire/bson/BsonEncoder.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public static byte determineType(Object value) {
184184
return BsonConstants.TYPE_BOOLEAN;
185185
} else if (value instanceof BinData || value instanceof UUID || value instanceof LegacyUUID) {
186186
return BsonConstants.TYPE_DATA;
187-
} else if (value instanceof Collection<?> || value instanceof String[]) {
187+
} else if (value instanceof Collection<?>) {
188188
return BsonConstants.TYPE_ARRAY;
189189
} else if (value instanceof Instant) {
190190
return BsonConstants.TYPE_UTC_DATETIME;
@@ -206,9 +206,7 @@ public static byte determineType(Object value) {
206206
}
207207

208208
private static List<?> collectionToList(Object value) {
209-
if (value instanceof String[]) {
210-
return List.of((String[]) value);
211-
} else if (value instanceof List<?>) {
209+
if (value instanceof List<?>) {
212210
return (List<?>) value;
213211
} else {
214212
return new ArrayList<>((Collection<?>) value);

core/src/test/java/de/bwaldvogel/mongo/backend/aggregation/ExpressionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,8 +1087,8 @@ void testEvaluateSlice() throws Exception {
10871087

10881088
@Test
10891089
void testEvaluateSplit() throws Exception {
1090-
assertThat((String[]) Expression.evaluate(json("$split: ['June-15-2013', '-']"), json(""))).containsExactly("June", "15", "2013");
1091-
assertThat((String[]) Expression.evaluate(json("$split: ['$a', '$b']"), json("a: 'foo bar', b: ' '"))).containsExactly("foo", "bar");
1090+
assertThat((List<String>) Expression.evaluate(json("$split: ['June-15-2013', '-']"), json(""))).containsExactly("June", "15", "2013");
1091+
assertThat((List<String>) Expression.evaluate(json("$split: ['$a', '$b']"), json("a: 'foo bar', b: ' '"))).containsExactly("foo", "bar");
10921092
assertThat(Expression.evaluate(json("$split: [null, ' ']"), json(""))).isNull();
10931093
assertThat(Expression.evaluate(json("$split: ['$doesNotExist', ' ']"), json(""))).isNull();
10941094

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,28 @@ void testAggregateWithSplit() throws Exception {
11771177
);
11781178
}
11791179

1180+
// https://github.com/bwaldvogel/mongo-java-server/issues/214
1181+
@Test
1182+
void testAggregateWithSplitAndArrayElementAt() throws Exception {
1183+
List<Document> pipeline = jsonList("$addFields: { pathSegments: { $split: ['$path', '/'] }}",
1184+
"$project: { pathSegments: 1, firstSegment: { $arrayElemAt: ['$pathSegments', 0] }}");
1185+
1186+
assertThat(collection.aggregate(pipeline)).isEmpty();
1187+
1188+
collection.insertOne(json("_id: 1, path: 'path/to/file'"));
1189+
collection.insertOne(json("_id: 2, path: '/path/to/file'"));
1190+
collection.insertOne(json("_id: 3, path: '/path/to/file/'"));
1191+
collection.insertOne(json("_id: 4"));
1192+
1193+
assertThat(collection.aggregate(pipeline))
1194+
.containsExactly(
1195+
json("_id: 1, pathSegments: ['path', 'to', 'file'], firstSegment: 'path'"),
1196+
json("_id: 2, pathSegments: ['', 'path', 'to', 'file'], firstSegment: ''"),
1197+
json("_id: 3, pathSegments: ['', 'path', 'to', 'file', ''], firstSegment: ''"),
1198+
json("_id: 4, pathSegments: null, firstSegment: null")
1199+
);
1200+
}
1201+
11801202
@Test
11811203
void testAggregateWithUnwind() throws Exception {
11821204
testAggregateWithUnwind(json("$unwind: '$sizes'"));

0 commit comments

Comments
 (0)