Skip to content

Commit 77bddd6

Browse files
committed
Ensure FindIterable.filter can handle nullable input
JAVA-3065
1 parent a7d2dc6 commit 77bddd6

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

driver-async/src/test/unit/com/mongodb/async/client/FindIterableSpecification.groovy

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import org.bson.codecs.BsonValueCodecProvider
3333
import org.bson.codecs.DocumentCodec
3434
import org.bson.codecs.DocumentCodecProvider
3535
import org.bson.codecs.ValueCodecProvider
36+
import org.bson.conversions.Bson
3637
import spock.lang.Specification
3738

3839
import static com.mongodb.CustomMatchers.isTheSameAs
@@ -58,7 +59,7 @@ class FindIterableSpecification extends Specification {
5859
it[0].onResult(null, null)
5960
}
6061
}
61-
def executor = new TestOperationExecutor([cursor, cursor])
62+
def executor = new TestOperationExecutor([cursor, cursor, cursor])
6263
def findIterable = new FindIterableImpl(null, namespace, Document, Document, codecRegistry, readPreference, readConcern, executor,
6364
new Document('filter', 1))
6465
.sort(new Document('sort', 1))
@@ -166,6 +167,26 @@ class FindIterableSpecification extends Specification {
166167
.showRecordId(true)
167168
.snapshot(true)
168169
)
170+
171+
when: 'passing nulls to nullable methods'
172+
new FindIterableImpl(null, namespace, Document, Document, codecRegistry, readPreference, readConcern, executor,
173+
new Document('filter', 1))
174+
.filter(null as Bson)
175+
.collation(null)
176+
.modifiers(null)
177+
.projection(null)
178+
.sort(null as Bson)
179+
.comment(null)
180+
.hint(null)
181+
.max(null as Bson)
182+
.min(null as Bson)
183+
.into([]) { result, t -> }
184+
185+
operation = executor.getReadOperation() as FindOperation<Document>
186+
187+
then: 'should set an empty doc for the filter'
188+
expect operation, isTheSameAs(new FindOperation<Document>(namespace, new DocumentCodec())
189+
.filter(new BsonDocument()).slaveOk(true))
169190
}
170191

171192
def 'should handle mixed types'() {

driver-core/src/main/com/mongodb/internal/operation/Operations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ <TResult> FindOperation<TResult> find(final MongoNamespace findNamespace, final
138138
private <TResult> FindOperation<TResult> createFindOperation(final MongoNamespace findNamespace, final Bson filter,
139139
final Class<TResult> resultClass, final FindOptions options) {
140140
return new FindOperation<TResult>(findNamespace, codecRegistry.get(resultClass))
141-
.filter(filter.toBsonDocument(documentClass, codecRegistry))
141+
.filter(filter == null ? new BsonDocument() : filter.toBsonDocument(documentClass, codecRegistry))
142142
.batchSize(options.getBatchSize())
143143
.skip(options.getSkip())
144144
.limit(options.getLimit())

driver-sync/src/test/unit/com/mongodb/client/internal/FindIterableSpecification.groovy

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.bson.codecs.BsonValueCodecProvider
3232
import org.bson.codecs.DocumentCodec
3333
import org.bson.codecs.DocumentCodecProvider
3434
import org.bson.codecs.ValueCodecProvider
35+
import org.bson.conversions.Bson
3536
import spock.lang.Specification
3637

3738
import static com.mongodb.CustomMatchers.isTheSameAs
@@ -53,7 +54,7 @@ class FindIterableSpecification extends Specification {
5354
@SuppressWarnings('deprecation')
5455
def 'should build the expected findOperation'() {
5556
given:
56-
def executor = new TestOperationExecutor([null, null])
57+
def executor = new TestOperationExecutor([null, null, null])
5758
def findIterable = new FindIterableImpl(null, namespace, Document, Document, codecRegistry, readPreference, readConcern,
5859
executor, new Document('filter', 1))
5960
.sort(new Document('sort', 1))
@@ -161,6 +162,26 @@ class FindIterableSpecification extends Specification {
161162
.showRecordId(true)
162163
.snapshot(true)
163164
)
165+
166+
when: 'passing nulls to nullable methods'
167+
new FindIterableImpl(null, namespace, Document, Document, codecRegistry, readPreference, readConcern,
168+
executor, new Document('filter', 1))
169+
.filter(null as Bson)
170+
.collation(null)
171+
.modifiers(null)
172+
.projection(null)
173+
.sort(null as Bson)
174+
.comment(null)
175+
.hint(null)
176+
.max(null as Bson)
177+
.min(null as Bson)
178+
.iterator()
179+
180+
operation = executor.getReadOperation() as FindOperation<Document>
181+
182+
then: 'should set an empty doc for the filter'
183+
expect operation, isTheSameAs(new FindOperation<Document>(namespace, new DocumentCodec())
184+
.filter(new BsonDocument()).slaveOk(true))
164185
}
165186

166187
def 'should use ClientSession'() {

0 commit comments

Comments
 (0)