title: Mono switchIfEmpty() is always called
body: <p>I have two methods.<br />
  Main method:</p>
  <pre><code>@PostMapping(&quot;/login&quot;)
  public Mono&lt;ResponseEntity&lt;ApiResponseLogin&gt;&gt; loginUser(@RequestBody final LoginUser loginUser) {
      return socialService.verifyAccount(loginUser)
              .flatMap(socialAccountIsValid -&gt; {
                  if (socialAccountIsValid) {
                      return this.userService.getUserByEmail(loginUser.getEmail())
                              .switchIfEmpty(insertUser(loginUser))
                              .flatMap(foundUser -&gt; updateUser(loginUser, foundUser))
                              .map(savedUser -&gt; {
                                  String jwts = jwt.createJwts(savedUser.get_id(), savedUser.getFirstName(), &quot;user&quot;);
                                  return new ResponseEntity&lt;&gt;(HttpStatus.OK);
                              });
                  } else {
                      return Mono.just(new ResponseEntity&lt;&gt;(HttpStatus.UNAUTHORIZED));
                  }
              });
  
  }
  </code></pre>
  <p>And this invoked method (the service calls an external api):</p>
  <pre><code>public Mono&lt;User&gt; getUserByEmail(String email) {
      UriComponentsBuilder builder = UriComponentsBuilder
              .fromHttpUrl(USER_API_BASE_URI)
              .queryParam(&quot;email&quot;, email);
      return this.webClient.get()
              .uri(builder.toUriString())
              .exchange()
              .flatMap(resp -&gt; {
                  if (Integer.valueOf(404).equals(resp.statusCode().value())) {
                      return Mono.empty();
                  } else {
                      return resp.bodyToMono(User.class);
                  }
              });
  } 
  </code></pre>
  <p>In the above example, <code>switchIfEmpty()</code> is always called from the main method, even when a result with <code>Mono.empty()</code> is returned.</p>
  <p>I cannot find a solution for this simple problem.<br />
  The following also doesn't work:</p>
  <pre><code>Mono.just(null) 
  </code></pre>
  <p>Because the method will throw a <code>NullPointerException</code>.</p>
  <p>What I also can't use is the flatMap method to check that <code>foundUser</code> is null.<br />
  Sadly, flatMap doesn't get called at all in case I return <code>Mono.empty()</code>, so I cannot add a condition here either.</p>
  <p>@SimY4</p>
  <pre><code>   @PostMapping(&quot;/login&quot;)
      public Mono&lt;ResponseEntity&lt;ApiResponseLogin&gt;&gt; loginUser(@RequestBody final LoginUser loginUser) {
          userExists = false;
          return socialService.verifyAccount(loginUser)
                  .flatMap(socialAccountIsValid -&gt; {
                      if (socialAccountIsValid) {
                          return this.userService.getUserByEmail(loginUser.getEmail())
                                  .flatMap(foundUser -&gt; {
                                      return updateUser(loginUser, foundUser);
                                  })
                                  .switchIfEmpty(Mono.defer(() -&gt; insertUser(loginUser)))
                                  .map(savedUser -&gt; {
                                      String jwts = jwt.createJwts(savedUser.get_id(), savedUser.getFirstName(), &quot;user&quot;);
                                      return new ResponseEntity&lt;&gt;(HttpStatus.OK);
                                  });
                      } else {
                          return Mono.just(new ResponseEntity&lt;&gt;(HttpStatus.UNAUTHORIZED));
                      }
                  });
  
      }
  </code></pre>