Skip to content

Commit

Permalink
Fix #2181 Handle nulls from collectors in Flux.collect()
Browse files Browse the repository at this point in the history
Emit an NPE early instead of signaling onNext(null).
  • Loading branch information
ericbottard committed Jun 10, 2020
1 parent 3daa029 commit 8de5bf3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public void onComplete() {
return;
}

if (r == null) {
actual.onError(Operators.onOperatorError(new NullPointerException("Collector returned null"), actual.currentContext()));
return;
}
complete(r);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import reactor.test.StepVerifier;
import reactor.test.subscriber.AssertSubscriber;

import static java.util.stream.Collectors.reducing;
import static org.assertj.core.api.Assertions.assertThat;

public class MonoStreamCollectorTest {
Expand Down Expand Up @@ -300,4 +301,14 @@ public void discardIntermediateMapOnFinisherFailure() {
assertThat(discardedMap).containsOnlyKeys(1, 2, 3, 4);
}

/**
* A collector producing null should be intercepted early instead of signaling onNext(null).
* @see <a href="https://github.com/reactor/reactor-core/issues/2181" target="_top">issue 2181</a>
*/
@Test
public void collectHandlesNulls() {
StepVerifier.create(Flux.empty().collect(reducing(null, (a, b) -> a)))
.verifyError(NullPointerException.class);
}

}

0 comments on commit 8de5bf3

Please sign in to comment.