Browse Source

Correctly detect Optional return type for @HttpExchange methods

Prior to this commit, a ClassCastException was thrown for an Optional
return type for an @HttpExchange method. This is because the check for
an Optional return type was based on the type contained in the Optional
instead of the Optional itself.

Closes gh-28493
pull/27579/merge
wonwoo 3 years ago committed by Sam Brannen
parent
commit
9181ac70f5
  1. 2
      spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java
  2. 8
      spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java

2
spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java

@ -324,7 +324,7 @@ final class HttpServiceMethod { @@ -324,7 +324,7 @@ final class HttpServiceMethod {
responseFunction = initBodyFunction(client, actualParam, reactiveAdapter);
}
boolean blockForOptional = actualType.equals(Optional.class);
boolean blockForOptional = returnType.equals(Optional.class);
return new ResponseFunction(responseFunction, reactiveAdapter, blockForOptional, blockTimeout);
}

8
spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java

@ -16,6 +16,8 @@ @@ -16,6 +16,8 @@
package org.springframework.web.service.invoker;
import java.util.Optional;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Single;
@ -131,6 +133,9 @@ public class HttpServiceMethodTests { @@ -131,6 +133,9 @@ public class HttpServiceMethodTests {
String body = service.getBody();
assertThat(body).isEqualTo("requestToBody");
Optional<String> optional = service.getBodyOptional();
assertThat(optional).isEqualTo(Optional.of("requestToBody"));
ResponseEntity<String> entity = service.getEntity();
assertThat(entity.getBody()).isEqualTo("requestToEntity");
@ -252,6 +257,9 @@ public class HttpServiceMethodTests { @@ -252,6 +257,9 @@ public class HttpServiceMethodTests {
@GetExchange
String getBody();
@GetExchange
Optional<String> getBodyOptional();
@GetExchange
ResponseEntity<Void> getVoidEntity();

Loading…
Cancel
Save