title: Mono switchIfEmpty is always called
body: <p>I have a bit more complex use case in Spring Gateway which is based on WebFlux and I ran into small issue with <code>Mono</code> usage. Long story short, my <code>switchIfEmpty</code> is called even if not required. I prepared small example which allows me to reproduce this problem:</p>
  <pre><code>public class ReactiveTest {
  
      @Test
      void test1() {
          isOdd(2)
              .flatMap(this::onNotEmpty)
              .switchIfEmpty(Mono.defer(this::onEmpty))
              .block();
      }
  
      Mono&lt;String&gt; isOdd(Integer number) {
          return number % 2 != 0 ? Mono.just(&quot;Yes&quot;) : Mono.empty();
      }
  
      Mono&lt;Void&gt; onNotEmpty(String value) {
          System.out.println(&quot;Value uppercased &quot; + value.toUpperCase());
          return Mono.empty();
      }
  
      Mono&lt;Void&gt; onEmpty() {
          System.out.println(&quot;Value not present, this shouldn't been called if value was odd&quot;);
          return Mono.empty();
      }
  }
  </code></pre>
  <p>I hope this is pretty self-explanatory, but just to be safe:</p>
  <ul>
  <li><code>isOdd(Integer number)</code> may produce <code>Mono</code> with data or empty <code>Mono</code></li>
  <li>I expect <code>onNotEmpty</code> to be called only if previous <code>Mono</code> had data</li>
  <li>I expect <code>onEmpty</code> to be called only if <code>isOdd</code> produced empty <code>Mono</code></li>
  </ul>
  <p>Unfortunatelly, both <code>onNotEmpty</code> and <code>onEmpty</code> are called all the time, regardless if I pass odd or even number to <code>isOdd</code>.</p>
  <p>How can I make sure that <code>onEmpty</code> is called only when <code>isOdd</code> produced <code>Mono.empty()</code>?</p>