Skip to content
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
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -80,7 +80,7 @@ public Object getKotlinLambdaTarget() {

@Override
public Object apply(Object input) {
if (ObjectUtils.isEmpty(input)) {
if (input == null) {
return this.invoke();
}
else {
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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<Function0<String>, String>' is matched by 'Function1'/'Function0'
return ResolvableType.forType(type).toClass() == clazz;
}

public Class<?> getObjectType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> function = value -> "[" + value + "]";
KotlinFunctionWrapper wrapper = new KotlinFunctionWrapper(function);

assertThat(wrapper.apply("")).isEqualTo("[]");
}

@Test
public void emptyValuesArePassedToFunction() {
Function1<Object, Object> function = value -> value;
KotlinFunctionWrapper wrapper = new KotlinFunctionWrapper(function);

List<Object> 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<String> 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<String, String>) 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<String, String>) 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<Function0<String>, 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down