Skip to content

Treat kotlin.Unit as void #31647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -27,6 +27,7 @@
*
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @author Yanming Zhou
* @since 5.0
*/
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -93,4 +94,19 @@ public static boolean isSuspendingFunction(Method method) {
return false;
}

/**
* Return {@code true} if the class is {@code kotlin.Unit}.
* @since 6.1.1
*/
public static boolean isKotlinUnit(Class<?> clazz) {
return "kotlin.Unit".equals(clazz.getName());
}

/**
* Return {@code true} if the object is a {@code kotlin.Unit}.
* @since 6.1.1
*/
public static boolean isKotlinUnit(@Nullable Object object) {
return (object != null && isKotlinUnit(object.getClass()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @author Yanming Zhou
* @since 3.1
*/
public class InvocableHandlerMethod extends HandlerMethod {
Expand Down Expand Up @@ -315,7 +316,11 @@ public static Object invokeFunction(Method method, Object target, Object[] args)
}
}
}
return function.callBy(argMap);
Object returnValue = function.callBy(argMap);
if (KotlinDetector.isKotlinUnit(returnValue)) {
returnValue = null;
}
return returnValue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletResponse
* Kotlin unit tests for {@link InvocableHandlerMethod}.
*
* @author Sebastien Deleuze
* @author Yanming Zhou
*/
class InvocableHandlerMethodKotlinTests {

Expand Down Expand Up @@ -71,6 +72,11 @@ class InvocableHandlerMethodKotlinTests {
Assertions.assertThat(value).isEqualTo("true")
}

@Test
fun shouldTreatUnitAsVoid() {
Assertions.assertThat(getInvocable().invokeForRequest(request, null)).isNull()
}

private fun getInvocable(vararg argTypes: Class<*>): InvocableHandlerMethod {
val method = ResolvableMethod.on(Handler::class.java).argTypes(*argTypes).resolveMethod()
val handlerMethod = InvocableHandlerMethod(Handler(), method)
Expand All @@ -95,6 +101,8 @@ class InvocableHandlerMethodKotlinTests {

fun nullableBooleanDefaultValue(status: Boolean? = true) =
status.toString()

fun returnUnit() {}
}

}