From 8400b162747554fab189f4c0e475b8435b0a5f90 Mon Sep 17 00:00:00 2001 From: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:23:10 +0900 Subject: [PATCH] GH-1537 Pass empty input to Kotlin functions and harden KotlinFunctionWrapper KotlinFunctionWrapper.apply(..) used ObjectUtils.isEmpty(input) to decide whether to invoke the target without an argument, so an empty String, collection, map, array or Optional was treated as no input. A Kotlin (String) -> String called with "" then went down the suspending supplier path and returned a Flux instead of the function result. Only null now means no input. Also in the same class: - strip only the trailing registration suffix from the bean name - fail with an IllegalStateException naming the function when its generic type can not be determined, instead of a NullPointerException or ClassCastException - compare raw types in isTypeRepresentedByClass instead of searching the type name, so Function1 no longer matches Function10 and a nested type argument such as Function1, String> is no longer registered as a Supplier Resolves #1537 Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com> --- ...tlinLambdaToFunctionAutoConfiguration.java | 22 +++- .../config/KotlinFunctionWrapperTests.java | 121 ++++++++++++++++++ ...onCatalogAutoConfigurationKotlinTests.java | 10 ++ 3 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/config/KotlinFunctionWrapperTests.java diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/KotlinLambdaToFunctionAutoConfiguration.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/KotlinLambdaToFunctionAutoConfiguration.java index 8c9419300..fa7a514b0 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/KotlinLambdaToFunctionAutoConfiguration.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/KotlinLambdaToFunctionAutoConfiguration.java @@ -18,6 +18,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.lang.reflect.WildcardType; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @@ -40,7 +41,6 @@ import org.springframework.cloud.function.context.catalog.FunctionTypeUtils; import org.springframework.context.annotation.Configuration; import org.springframework.core.ResolvableType; -import org.springframework.util.ObjectUtils; /** * Configuration class which defines the required infrastructure to bootstrap Kotlin @@ -80,7 +80,7 @@ public Object getKotlinLambdaTarget() { @Override public Object apply(Object input) { - if (ObjectUtils.isEmpty(input)) { + if (input == null) { return this.invoke(); } else { @@ -145,11 +145,15 @@ public Object get() { public FunctionRegistration getFunctionRegistration() { String name = this.name.endsWith(FunctionRegistration.REGISTRATION_NAME_SUFFIX) - ? this.name.replace(FunctionRegistration.REGISTRATION_NAME_SUFFIX, "") + ? this.name.substring(0, this.name.length() - FunctionRegistration.REGISTRATION_NAME_SUFFIX.length()) : this.name; Type functionType = FunctionContextUtils.findType(name, this.beanFactory); + if (!(functionType instanceof ParameterizedType parameterizedType)) { + throw new IllegalStateException("Failed to determine the generic type of Kotlin function '" + name + + "', discovered type is " + functionType); + } FunctionRegistration registration = new FunctionRegistration<>(this, name); - Type[] types = ((ParameterizedType) functionType).getActualTypeArguments(); + Type[] types = parameterizedType.getActualTypeArguments(); if (isValidKotlinSupplier(functionType)) { functionType = ResolvableType.forClassWithGenerics(Supplier.class, ResolvableType.forType(types[0])) @@ -233,7 +237,15 @@ private boolean isValidKotlinSuspendFunction(Type functionType, Type[] type) { } private boolean isTypeRepresentedByClass(Type type, Class clazz) { - return type.getTypeName().contains(clazz.getName()); + if (type instanceof WildcardType wildcardType) { + Type[] bounds = wildcardType.getLowerBounds().length > 0 + ? wildcardType.getLowerBounds() + : wildcardType.getUpperBounds(); + type = bounds[0]; + } + // compare the raw type rather than the type name so that neither 'Function10' nor a nested + // type argument such as 'Function1, String>' is matched by 'Function1'/'Function0' + return ResolvableType.forType(type).toClass() == clazz; } public Class getObjectType() { diff --git a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/config/KotlinFunctionWrapperTests.java b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/config/KotlinFunctionWrapperTests.java new file mode 100644 index 000000000..a360b68dc --- /dev/null +++ b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/context/config/KotlinFunctionWrapperTests.java @@ -0,0 +1,121 @@ +/* + * Copyright 2026-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.function.context.config; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.function.Function; + +import kotlin.jvm.functions.Function0; +import kotlin.jvm.functions.Function1; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.cloud.function.context.FunctionRegistration; +import org.springframework.cloud.function.context.catalog.FunctionTypeUtils; +import org.springframework.cloud.function.context.config.KotlinLambdaToFunctionAutoConfiguration.KotlinFunctionWrapper; +import org.springframework.core.ResolvableType; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; + +/** + * Tests for {@link KotlinFunctionWrapper}. + * + * @author Seonwoo Jung + */ +public class KotlinFunctionWrapperTests { + + @Test + public void emptyStringInputIsPassedToFunction() { + Function1 function = value -> "[" + value + "]"; + KotlinFunctionWrapper wrapper = new KotlinFunctionWrapper(function); + + assertThat(wrapper.apply("")).isEqualTo("[]"); + } + + @Test + public void emptyValuesArePassedToFunction() { + Function1 function = value -> value; + KotlinFunctionWrapper wrapper = new KotlinFunctionWrapper(function); + + List emptyList = Collections.emptyList(); + assertThat(wrapper.apply(emptyList)).isSameAs(emptyList); + assertThat(wrapper.apply(Collections.emptyMap())).isEqualTo(Collections.emptyMap()); + assertThat(wrapper.apply(Optional.empty())).isEqualTo(Optional.empty()); + Object[] emptyArray = new Object[0]; + assertThat(wrapper.apply(emptyArray)).isSameAs(emptyArray); + } + + @Test + public void nullInputInvokesSupplier() { + Function0 supplier = () -> "hello"; + KotlinFunctionWrapper wrapper = new KotlinFunctionWrapper(supplier); + + assertThat(wrapper.apply(null)).isEqualTo("hello"); + assertThat(wrapper.get()).isEqualTo("hello"); + } + + @Test + public void onlyTrailingRegistrationSuffixIsRemoved() { + String beanName = "event" + FunctionRegistration.REGISTRATION_NAME_SUFFIX; + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + RootBeanDefinition definition = new RootBeanDefinition(); + definition.setTargetType(ResolvableType.forClassWithGenerics(Function1.class, String.class, String.class)); + beanFactory.registerBeanDefinition(beanName, definition); + + KotlinFunctionWrapper wrapper = new KotlinFunctionWrapper((Function1) value -> value); + wrapper.setName(beanName + FunctionRegistration.REGISTRATION_NAME_SUFFIX); + wrapper.setBeanFactory(beanFactory); + + FunctionRegistration registration = wrapper.getFunctionRegistration(); + assertThat(registration.getNames()).containsExactly(beanName); + assertThat(FunctionTypeUtils.isFunction(registration.getType())).isTrue(); + } + + @Test + public void nonParameterizedFunctionTypeIsReported() { + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + KotlinFunctionWrapper wrapper = new KotlinFunctionWrapper((Function1) value -> value); + wrapper.setName("unknown"); + wrapper.setBeanFactory(beanFactory); + + assertThatIllegalStateException().isThrownBy(wrapper::getFunctionRegistration) + .withMessageContaining("'unknown'"); + } + + @Test + public void functionTakingSupplierIsNotMistakenForSupplier() { + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + RootBeanDefinition definition = new RootBeanDefinition(); + definition.setTargetType(ResolvableType.forClassWithGenerics(Function1.class, + ResolvableType.forClassWithGenerics(Function0.class, String.class), ResolvableType.forClass(String.class))); + beanFactory.registerBeanDefinition("invoker", definition); + + KotlinFunctionWrapper wrapper = new KotlinFunctionWrapper((Function1, String>) Function0::invoke); + wrapper.setName("invoker"); + wrapper.setBeanFactory(beanFactory); + + ResolvableType type = ResolvableType.forType(wrapper.getFunctionRegistration().getType()); + assertThat(type.toClass()).isEqualTo(Function.class); + assertThat(type.getGeneric(0).toClass()).isEqualTo(Function0.class); + assertThat(type.getGeneric(1).toClass()).isEqualTo(String.class); + } + +} diff --git a/spring-cloud-function-kotlin/src/test/java/org/springframework/cloud/function/kotlin/ContextFunctionCatalogAutoConfigurationKotlinTests.java b/spring-cloud-function-kotlin/src/test/java/org/springframework/cloud/function/kotlin/ContextFunctionCatalogAutoConfigurationKotlinTests.java index 606454e98..a677c99e8 100644 --- a/spring-cloud-function-kotlin/src/test/java/org/springframework/cloud/function/kotlin/ContextFunctionCatalogAutoConfigurationKotlinTests.java +++ b/spring-cloud-function-kotlin/src/test/java/org/springframework/cloud/function/kotlin/ContextFunctionCatalogAutoConfigurationKotlinTests.java @@ -172,6 +172,16 @@ public void kotlinLambdas() { .isEqualTo("Hello"); } + @Test + public void kotlinFunctionWithEmptyInput() { + create(new Class[] { KotlinLambdasConfiguration.class, + SimpleConfiguration.class }); + + FunctionInvocationWrapper function = this.catalog.lookup(Function.class, "kotlinFunction"); + assertThat(function.apply("")).isEqualTo(""); + assertThat(function.apply("hello")).isEqualTo("HELLO"); + } + private void create(Class[] types, String... props) { this.context = (GenericApplicationContext) new SpringApplicationBuilder(types).properties(props).run(); this.catalog = this.context.getBean(FunctionCatalog.class);