Skip to content

Commit 56b3da2

Browse files
committed
1 parent 5a3ad6b commit 56b3da2

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

spring-core/src/main/java/org/springframework/core/KotlinDetector.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
*
2828
* @author Juergen Hoeller
2929
* @author Sebastien Deleuze
30+
* @author Yanming Zhou
3031
* @since 5.0
3132
*/
3233
@SuppressWarnings("unchecked")
@@ -93,4 +94,19 @@ public static boolean isSuspendingFunction(Method method) {
9394
return false;
9495
}
9596

97+
/**
98+
* Return {@code true} if the class is {@code kotlin.Unit}.
99+
* @since 6.1.1
100+
*/
101+
public static boolean isKotlinUnit(Class<?> clazz) {
102+
return "kotlin.Unit".equals(clazz.getName());
103+
}
104+
105+
/**
106+
* Return {@code true} if the object is a {@code kotlin.Unit}.
107+
* @since 6.1.1
108+
*/
109+
public static boolean isKotlinUnit(@Nullable Object object) {
110+
return (object != null && isKotlinUnit(object.getClass()));
111+
}
96112
}

spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @author Rossen Stoyanchev
5252
* @author Juergen Hoeller
5353
* @author Sebastien Deleuze
54+
* @author Yanming Zhou
5455
* @since 3.1
5556
*/
5657
public class InvocableHandlerMethod extends HandlerMethod {
@@ -315,7 +316,11 @@ public static Object invokeFunction(Method method, Object target, Object[] args)
315316
}
316317
}
317318
}
318-
return function.callBy(argMap);
319+
Object returnValue = function.callBy(argMap);
320+
if (KotlinDetector.isKotlinUnit(returnValue)) {
321+
returnValue = null;
322+
}
323+
return returnValue;
319324
}
320325
}
321326

spring-web/src/test/kotlin/org/springframework/web/method/support/InvocableHandlerMethodKotlinTests.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse
2828
* Kotlin unit tests for {@link InvocableHandlerMethod}.
2929
*
3030
* @author Sebastien Deleuze
31+
* @author Yanming Zhou
3132
*/
3233
class InvocableHandlerMethodKotlinTests {
3334

@@ -71,6 +72,11 @@ class InvocableHandlerMethodKotlinTests {
7172
Assertions.assertThat(value).isEqualTo("true")
7273
}
7374

75+
@Test
76+
fun shouldTreatUnitAsVoid() {
77+
Assertions.assertThat(getInvocable().invokeForRequest(request, null)).isNull()
78+
}
79+
7480
private fun getInvocable(vararg argTypes: Class<*>): InvocableHandlerMethod {
7581
val method = ResolvableMethod.on(Handler::class.java).argTypes(*argTypes).resolveMethod()
7682
val handlerMethod = InvocableHandlerMethod(Handler(), method)
@@ -95,6 +101,8 @@ class InvocableHandlerMethodKotlinTests {
95101

96102
fun nullableBooleanDefaultValue(status: Boolean? = true) =
97103
status.toString()
104+
105+
fun returnUnit() {}
98106
}
99107

100108
}

0 commit comments

Comments
 (0)