Skip to content

Commit 3e7d63c

Browse files
vanniktechakarnokd
authored andcommitted
2.x: Delegate null Collections down to onError in toList (#4731)
1 parent 9e18df6 commit 3e7d63c

File tree

6 files changed

+71
-6
lines changed

6 files changed

+71
-6
lines changed

src/main/java/io/reactivex/internal/operators/flowable/FlowableToList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package io.reactivex.internal.operators.flowable;
1515

16+
import io.reactivex.internal.functions.ObjectHelper;
1617
import java.util.Collection;
1718
import java.util.concurrent.Callable;
1819

@@ -33,7 +34,7 @@ public FlowableToList(Publisher<T> source, Callable<U> collectionSupplier) {
3334
protected void subscribeActual(Subscriber<? super U> s) {
3435
U coll;
3536
try {
36-
coll = collectionSupplier.call();
37+
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
3738
} catch (Throwable e) {
3839
Exceptions.throwIfFatal(e);
3940
EmptySubscription.error(e, s);

src/main/java/io/reactivex/internal/operators/flowable/FlowableToListSingle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package io.reactivex.internal.operators.flowable;
1515

16+
import io.reactivex.internal.functions.ObjectHelper;
1617
import java.util.Collection;
1718
import java.util.concurrent.Callable;
1819

@@ -47,7 +48,7 @@ public FlowableToListSingle(Publisher<T> source, Callable<U> collectionSupplier)
4748
protected void subscribeActual(SingleObserver<? super U> s) {
4849
U coll;
4950
try {
50-
coll = collectionSupplier.call();
51+
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
5152
} catch (Throwable e) {
5253
Exceptions.throwIfFatal(e);
5354
EmptyDisposable.error(e, s);

src/main/java/io/reactivex/internal/operators/observable/ObservableToList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public ObservableToList(ObservableSource<T> source, Callable<U> collectionSuppli
4242
public void subscribeActual(Observer<? super U> t) {
4343
U coll;
4444
try {
45-
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null Collection");
45+
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
4646
} catch (Throwable e) {
4747
Exceptions.throwIfFatal(e);
4848
EmptyDisposable.error(e, t);

src/main/java/io/reactivex/internal/operators/observable/ObservableToListSingle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package io.reactivex.internal.operators.observable;
1515

16+
import io.reactivex.internal.functions.ObjectHelper;
1617
import java.util.*;
1718
import java.util.concurrent.Callable;
1819

@@ -48,7 +49,7 @@ public ObservableToListSingle(ObservableSource<T> source, Callable<U> collection
4849
public void subscribeActual(SingleObserver<? super U> t) {
4950
U coll;
5051
try {
51-
coll = collectionSupplier.call();
52+
coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
5253
} catch (Throwable e) {
5354
Exceptions.throwIfFatal(e);
5455
EmptyDisposable.error(e, t);

src/test/java/io/reactivex/internal/operators/flowable/FlowableToListTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,22 @@ public Collection<Integer> call() throws Exception {
345345
.assertFailure(TestException.class);
346346
}
347347

348+
@SuppressWarnings("unchecked")
349+
@Test
350+
public void collectionSupplierReturnsNull() {
351+
Flowable.just(1)
352+
.toList(new Callable<Collection<Integer>>() {
353+
@Override
354+
public Collection<Integer> call() throws Exception {
355+
return null;
356+
}
357+
})
358+
.toFlowable()
359+
.test()
360+
.assertFailure(NullPointerException.class)
361+
.assertErrorMessage("The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
362+
}
363+
348364
@SuppressWarnings("unchecked")
349365
@Test
350366
public void singleCollectionSupplierThrows() {
@@ -358,4 +374,19 @@ public Collection<Integer> call() throws Exception {
358374
.test()
359375
.assertFailure(TestException.class);
360376
}
361-
}
377+
378+
@SuppressWarnings("unchecked")
379+
@Test
380+
public void singleCollectionSupplierReturnsNull() {
381+
Flowable.just(1)
382+
.toList(new Callable<Collection<Integer>>() {
383+
@Override
384+
public Collection<Integer> call() throws Exception {
385+
return null;
386+
}
387+
})
388+
.test()
389+
.assertFailure(NullPointerException.class)
390+
.assertErrorMessage("The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
391+
}
392+
}

src/test/java/io/reactivex/internal/operators/observable/ObservableToListTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,22 @@ public Collection<Integer> call() throws Exception {
226226
.assertFailure(TestException.class);
227227
}
228228

229+
@SuppressWarnings("unchecked")
230+
@Test
231+
public void collectionSupplierReturnsNull() {
232+
Observable.just(1)
233+
.toList(new Callable<Collection<Integer>>() {
234+
@Override
235+
public Collection<Integer> call() throws Exception {
236+
return null;
237+
}
238+
})
239+
.toObservable()
240+
.test()
241+
.assertFailure(NullPointerException.class)
242+
.assertErrorMessage("The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
243+
}
244+
229245
@SuppressWarnings("unchecked")
230246
@Test
231247
public void singleCollectionSupplierThrows() {
@@ -239,4 +255,19 @@ public Collection<Integer> call() throws Exception {
239255
.test()
240256
.assertFailure(TestException.class);
241257
}
242-
}
258+
259+
@SuppressWarnings("unchecked")
260+
@Test
261+
public void singleCollectionSupplierReturnsNull() {
262+
Observable.just(1)
263+
.toList(new Callable<Collection<Integer>>() {
264+
@Override
265+
public Collection<Integer> call() throws Exception {
266+
return null;
267+
}
268+
})
269+
.test()
270+
.assertFailure(NullPointerException.class)
271+
.assertErrorMessage("The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources.");
272+
}
273+
}

0 commit comments

Comments
 (0)