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);