Skip to content

Commit cd84425

Browse files
committed
Fix NPE bug in AggregateIterable
JAVA-2019
1 parent fdb999c commit cd84425

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

driver/src/main/com/mongodb/AggregateIterableImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb;
1818

1919
import com.mongodb.client.AggregateIterable;
20+
import com.mongodb.client.FindIterable;
2021
import com.mongodb.client.MongoCursor;
2122
import com.mongodb.client.MongoIterable;
2223
import com.mongodb.client.model.FindOptions;
@@ -122,11 +123,15 @@ private MongoIterable<TResult> execute() {
122123
.maxTime(maxTimeMS, MILLISECONDS)
123124
.allowDiskUse(allowDiskUse);
124125
executor.execute(operation);
125-
return new FindIterableImpl<TDocument, TResult>(new MongoNamespace(namespace.getDatabaseName(),
126+
FindIterable<TResult> findOperation = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(namespace.getDatabaseName(),
126127
outCollection.asString().getValue()),
127128
documentClass, resultClass, codecRegistry, readPreference, executor,
128129
new BsonDocument(),
129-
new FindOptions()).batchSize(batchSize);
130+
new FindOptions());
131+
if (batchSize != null) {
132+
findOperation.batchSize(batchSize);
133+
}
134+
return findOperation;
130135
} else {
131136
return new OperationIterable<TResult>(new AggregateOperation<TResult>(namespace, aggregateList, codecRegistry.get(resultClass))
132137
.maxTime(maxTimeMS, MILLISECONDS)

driver/src/test/functional/com/mongodb/client/MongoCollectionTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@
2929
import org.junit.Test;
3030

3131
import java.util.ArrayList;
32+
import java.util.Collections;
3233
import java.util.List;
3334

35+
import static com.mongodb.ClusterFixture.serverVersionAtLeast;
3436
import static java.util.Arrays.asList;
3537
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
3638
import static org.hamcrest.CoreMatchers.is;
3739
import static org.junit.Assert.assertEquals;
3840
import static org.junit.Assert.assertNotNull;
3941
import static org.junit.Assert.assertThat;
42+
import static org.junit.Assume.assumeTrue;
4043

4144
public class MongoCollectionTest extends DatabaseTestCase {
4245

@@ -137,6 +140,26 @@ public void testMapReduceWithGenerics() {
137140
assertEquals(new Name("Sam", 1), result.get(1));
138141
}
139142

143+
@Test
144+
public void testAggregationToACollection() {
145+
assumeTrue(serverVersionAtLeast(asList(2, 6, 0)));
146+
147+
// given
148+
List<Document> documents = asList(new Document("_id", 1), new Document("_id", 2));
149+
150+
getCollectionHelper().insertDocuments(new DocumentCodec(), documents);
151+
152+
MongoCollection<Document> collection = database
153+
.getCollection(getCollectionName());
154+
155+
// when
156+
List<Document> result = collection.aggregate(Collections.singletonList(new Document("$out", "outCollection")))
157+
.into(new ArrayList<Document>());
158+
159+
// then
160+
assertEquals(documents, result);
161+
}
162+
140163
// This is really a test that the default registry created in MongoClient and passed down to MongoCollection has been constructed
141164
// properly to handle DBRef encoding and decoding
142165
@Test

0 commit comments

Comments
 (0)