From 9181ac70f52951d6b2eba787f2c5d51cd48191ed Mon Sep 17 00:00:00 2001 From: wonwoo Date: Fri, 20 May 2022 13:31:52 +0900 Subject: [PATCH] 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 --- .../web/service/invoker/HttpServiceMethod.java | 2 +- .../web/service/invoker/HttpServiceMethodTests.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java b/spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java index 81e3758b9b1..b6307045b8c 100644 --- a/spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java +++ b/spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceMethod.java @@ -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); } diff --git a/spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java b/spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java index f61bbbd82c4..2802a1ed01f 100644 --- a/spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java +++ b/spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java @@ -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 { String body = service.getBody(); assertThat(body).isEqualTo("requestToBody"); + Optional optional = service.getBodyOptional(); + assertThat(optional).isEqualTo(Optional.of("requestToBody")); + ResponseEntity entity = service.getEntity(); assertThat(entity.getBody()).isEqualTo("requestToEntity"); @@ -252,6 +257,9 @@ public class HttpServiceMethodTests { @GetExchange String getBody(); + @GetExchange + Optional getBodyOptional(); + @GetExchange ResponseEntity getVoidEntity();