Skip to content

Commit 8240d5c

Browse files
kojilinakarnokd
authored andcommitted
2.x: make SingleMap not allow map function return null (#5378)
1 parent c507577 commit 8240d5c

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/main/java/io/reactivex/internal/operators/single/SingleMap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.reactivex.disposables.Disposable;
1818
import io.reactivex.exceptions.Exceptions;
1919
import io.reactivex.functions.Function;
20+
import io.reactivex.internal.functions.ObjectHelper;
2021

2122
public final class SingleMap<T, R> extends Single<R> {
2223
final SingleSource<? extends T> source;
@@ -53,7 +54,7 @@ public void onSubscribe(Disposable d) {
5354
public void onSuccess(T value) {
5455
R v;
5556
try {
56-
v = mapper.apply(value);
57+
v = ObjectHelper.requireNonNull(mapper.apply(value), "The mapper function returned a null value.");
5758
} catch (Throwable e) {
5859
Exceptions.throwIfFatal(e);
5960
onError(e);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex.internal.operators.single;
15+
16+
import org.junit.Test;
17+
18+
import io.reactivex.Single;
19+
import io.reactivex.SingleSource;
20+
import io.reactivex.functions.Function;
21+
22+
public class SingleMapTest {
23+
24+
@Test(expected = NullPointerException.class)
25+
public void mapNull() {
26+
Single.just(1).map(null);
27+
}
28+
29+
@Test
30+
public void mapValue() {
31+
Single.just(1).map(new Function<Integer, Integer>() {
32+
@Override
33+
public Integer apply(final Integer integer) throws Exception {
34+
if (integer == 1) {
35+
return 2;
36+
}
37+
38+
return 1;
39+
}
40+
})
41+
.test()
42+
.assertResult(2);
43+
}
44+
45+
@Test
46+
public void mapValueNull() {
47+
Single.just(1).map(new Function<Integer, SingleSource<Integer>>() {
48+
@Override
49+
public SingleSource<Integer> apply(final Integer integer) throws Exception {
50+
return null;
51+
}
52+
})
53+
.test()
54+
.assertNoValues()
55+
.assertError(NullPointerException.class)
56+
.assertErrorMessage("The mapper function returned a null value.");
57+
}
58+
59+
@Test
60+
public void mapValueErrorThrown() {
61+
Single.just(1).map(new Function<Integer, SingleSource<Integer>>() {
62+
@Override
63+
public SingleSource<Integer> apply(final Integer integer) throws Exception {
64+
throw new RuntimeException("something went terribly wrong!");
65+
}
66+
})
67+
.test()
68+
.assertNoValues()
69+
.assertError(RuntimeException.class)
70+
.assertErrorMessage("something went terribly wrong!");
71+
}
72+
73+
@Test
74+
public void mapError() {
75+
RuntimeException exception = new RuntimeException("test");
76+
77+
Single.error(exception).map(new Function<Object, Object>() {
78+
@Override
79+
public Object apply(final Object integer) throws Exception {
80+
return new Object();
81+
}
82+
})
83+
.test()
84+
.assertError(exception);
85+
}
86+
}

0 commit comments

Comments
 (0)