79532339

Date: 2025-03-24 23:01:09
Score: 2.5
Natty:
Report link

What happened is that the collectList returned a Mono with an empty list, but not a Mono.empty, so we have:

...
.collectList()
.filter(list -> !list.isEmpty())

or

...
.collectList()
.flatMap(list -> !list.isEmpty() ? Mono.just(list) : Mono.empty())

With this we successfully force a Mono.empty() that will trigger your switchIfEmpty().

I show you an example testing with StepVerifier.

<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-test</artifactId>
    <scope>test</scope>
    <version>3.7.0</version>
</dependency>

You can decomment the flatMap to see how it works as well.

class ReactiveTest {

    @Test
    @DisplayName("expecting lowerCase names when filter(__ -> names.size() > 5)")
    void expectingLowerCase() {
        List<String> names = Arrays.asList("google", "abc", "fb", "stackoverflow");

        StepVerifier.create(Flux.fromIterable(names)
                        .filter(name -> name.length() > 5)
                        .collectList()
                        .flatMap(commonAppliedFilters ->
                                Mono.just(commonAppliedFilters)
                                        .filter(__ -> names.size() > 5)
                                        .flatMapIterable(list -> list)
                                        .map(String::toUpperCase)
                                        .collectList()
                                        //.flatMap(list -> !list.isEmpty() ? Mono.just(list) : Mono.empty())
                                        .filter(list -> !list.isEmpty())
                                        // If the list is not empty, a Mono.empty() is returned which will force switchIfEmpty
                                        .switchIfEmpty(Mono.defer(() -> Mono.just(commonAppliedFilters)))
                        ))
                .expectNext(List.of("google", "stackoverflow"))
                .verifyComplete();
    }

    @Test
    @DisplayName("expecting upperCase names when filter(__ -> names.size() > 3) ")
    void expectingUpperCase() {
        List<String> names = Arrays.asList("google", "abc", "fb", "stackoverflow");

        StepVerifier.create(Flux.fromIterable(names)
                        .filter(name -> name.length() > 5)
                        .collectList()
                        .flatMap(commonAppliedFilters ->
                                Mono.just(commonAppliedFilters)
                                        .filter(__ -> names.size() > 3)
                                        .flatMapIterable(list -> list)
                                        .map(String::toUpperCase)
                                        .collectList()
//                                        .flatMap(list -> !list.isEmpty() ? Mono.just(list) : Mono.empty())
                                        .filter(list -> !list.isEmpty())
                                        .switchIfEmpty(Mono.defer(() -> Mono.just(commonAppliedFilters)))
                        ))
                .expectNext(List.of("GOOGLE", "STACKOVERFLOW"))
                .verifyComplete();
    }

}

enter image description here

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Blacklisted phrase (1): STACKOVERFLOW
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Starts with a question (0.5): What
  • Low reputation (0.5):
Posted by: 0x52