Skip to content

Commit 9181ac7

Browse files
wonwoosbrannen
authored andcommitted
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
1 parent 9487e8c commit 9181ac7

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ else if (actualType.equals(ResponseEntity.class)) {
324324
responseFunction = initBodyFunction(client, actualParam, reactiveAdapter);
325325
}
326326

327-
boolean blockForOptional = actualType.equals(Optional.class);
327+
boolean blockForOptional = returnType.equals(Optional.class);
328328
return new ResponseFunction(responseFunction, reactiveAdapter, blockForOptional, blockTimeout);
329329
}
330330

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.web.service.invoker;
1818

19+
import java.util.Optional;
20+
1921
import io.reactivex.rxjava3.core.Completable;
2022
import io.reactivex.rxjava3.core.Flowable;
2123
import io.reactivex.rxjava3.core.Single;
@@ -131,6 +133,9 @@ void blockingService() {
131133
String body = service.getBody();
132134
assertThat(body).isEqualTo("requestToBody");
133135

136+
Optional<String> optional = service.getBodyOptional();
137+
assertThat(optional).isEqualTo(Optional.of("requestToBody"));
138+
134139
ResponseEntity<String> entity = service.getEntity();
135140
assertThat(entity.getBody()).isEqualTo("requestToEntity");
136141

@@ -252,6 +257,9 @@ private interface BlockingService {
252257
@GetExchange
253258
String getBody();
254259

260+
@GetExchange
261+
Optional<String> getBodyOptional();
262+
255263
@GetExchange
256264
ResponseEntity<Void> getVoidEntity();
257265

0 commit comments

Comments
 (0)