Support suspending functions returning Unit in WebFlux
Closes gh-27629
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -129,15 +129,16 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
||||
* @param providedArgs optional list of argument values to match by type
|
||||
* @return a Mono with a {@link HandlerResult}
|
||||
*/
|
||||
@SuppressWarnings("KotlinInternalInJava")
|
||||
@SuppressWarnings({"KotlinInternalInJava", "unchecked"})
|
||||
public Mono<HandlerResult> invoke(
|
||||
ServerWebExchange exchange, BindingContext bindingContext, Object... providedArgs) {
|
||||
|
||||
return getMethodArgumentValues(exchange, bindingContext, providedArgs).flatMap(args -> {
|
||||
Object value;
|
||||
Method method = getBridgedMethod();
|
||||
boolean isSuspendingFunction = KotlinDetector.isSuspendingFunction(method);
|
||||
try {
|
||||
Method method = getBridgedMethod();
|
||||
if (KotlinDetector.isSuspendingFunction(method)) {
|
||||
if (isSuspendingFunction) {
|
||||
value = CoroutinesUtils.invokeSuspendingFunction(method, getBean(), args);
|
||||
}
|
||||
else {
|
||||
@@ -163,10 +164,16 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
||||
}
|
||||
|
||||
MethodParameter returnType = getReturnType();
|
||||
ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(returnType.getParameterType());
|
||||
boolean asyncVoid = isAsyncVoidReturnType(returnType, adapter);
|
||||
if ((value == null || asyncVoid) && isResponseHandled(args, exchange)) {
|
||||
return (asyncVoid ? Mono.from(adapter.toPublisher(value)) : Mono.empty());
|
||||
if (isResponseHandled(args, exchange)) {
|
||||
Class<?> parameterType = returnType.getParameterType();
|
||||
ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(parameterType);
|
||||
boolean asyncVoid = isAsyncVoidReturnType(returnType, adapter);
|
||||
if (value == null || asyncVoid) {
|
||||
return (asyncVoid ? Mono.from(adapter.toPublisher(value)) : Mono.empty());
|
||||
}
|
||||
if (isSuspendingFunction && parameterType == void.class) {
|
||||
return (Mono<HandlerResult>) value;
|
||||
}
|
||||
}
|
||||
|
||||
HandlerResult result = new HandlerResult(this, value, returnType, bindingContext);
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.springframework.web.reactive.result.method.annotation.ContinuationHan
|
||||
import reactor.core.publisher.Mono
|
||||
import reactor.test.StepVerifier
|
||||
import java.lang.reflect.Method
|
||||
import java.time.Duration
|
||||
import kotlin.reflect.jvm.javaMethod
|
||||
|
||||
class KotlinInvocableHandlerMethodTests {
|
||||
@@ -89,11 +90,9 @@ class KotlinInvocableHandlerMethodTests {
|
||||
val response = this.exchange.response
|
||||
this.resolvers.add(stubResolver(response))
|
||||
val method = CoroutinesController::response.javaMethod!!
|
||||
val result = invoke(CoroutinesController(), method)
|
||||
val result = invokeForResult(CoroutinesController(), method, response)
|
||||
|
||||
StepVerifier.create(result)
|
||||
.consumeNextWith { StepVerifier.create(it.returnValue as Mono<*>).verifyComplete() }
|
||||
.verifyComplete()
|
||||
assertThat(result).`as`("Expected no result (i.e. fully handled)").isNull()
|
||||
assertThat(this.exchange.response.headers.getFirst("foo")).isEqualTo("bar")
|
||||
}
|
||||
|
||||
@@ -105,6 +104,10 @@ class KotlinInvocableHandlerMethodTests {
|
||||
assertHandlerResultValue(result, "success:foo")
|
||||
}
|
||||
|
||||
private fun invokeForResult(handler: Any, method: Method, vararg providedArgs: Any): HandlerResult? {
|
||||
return invoke(handler, method, *providedArgs).block(Duration.ofSeconds(5))
|
||||
}
|
||||
|
||||
private fun invoke(handler: Any, method: Method, vararg providedArgs: Any?): Mono<HandlerResult> {
|
||||
val invocable = InvocableHandlerMethod(handler, method)
|
||||
invocable.setArgumentResolvers(this.resolvers)
|
||||
|
||||
Reference in New Issue
Block a user