Skip to content

Commit 227f070

Browse files
committed
Fix: function calling not able to resolve input types
Resolves #726
1 parent 82f473f commit 227f070

File tree

3 files changed

+132
-6
lines changed

3 files changed

+132
-6
lines changed

spring-ai-core/src/main/java/org/springframework/ai/model/function/TypeResolverHelper.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import net.jodah.typetools.TypeResolver;
2424

25+
import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
26+
2527
/**
2628
* @author Christian Tzolov
2729
*/
@@ -58,6 +60,12 @@ public static Type getFunctionArgumentType(Class<? extends Function<?, ?>> funct
5860
}
5961

6062
public static Type getFunctionArgumentType(Type functionType, int argumentIndex) {
63+
64+
// Resolves: https://github.com/spring-projects/spring-ai/issues/726
65+
if (!(functionType instanceof ParameterizedType)) {
66+
functionType = FunctionTypeUtils.discoverFunctionTypeFromClass(FunctionTypeUtils.getRawType(functionType));
67+
}
68+
6169
var argumentType = functionType instanceof ParameterizedType
6270
? ((ParameterizedType) functionType).getActualTypeArguments()[argumentIndex] : Object.class;
6371

@@ -77,10 +85,4 @@ public static Class<?> toRawClass(Type type) {
7785
: null;
7886
}
7987

80-
// public static void main(String[] args) {
81-
// Class<? extends Function<?, ?>> clazz = MockWeatherService.class;
82-
// System.out.println(getFunctionInputType(clazz));
83-
84-
// }
85-
8688
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.model.function;
18+
19+
import java.util.function.Function;
20+
21+
import org.springframework.ai.model.function.TypeResolverHelperIT.WeatherRequest;
22+
import org.springframework.ai.model.function.TypeResolverHelperIT.WeatherResponse;
23+
24+
/**
25+
* @author Christian Tzolov
26+
*/
27+
public class StandaloneWeatherFunction implements Function<WeatherRequest, WeatherResponse> {
28+
29+
@Override
30+
public WeatherResponse apply(WeatherRequest weatherRequest) {
31+
return new WeatherResponse(42.0f);
32+
}
33+
34+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2023 - 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.ai.model.function;
17+
18+
import java.lang.reflect.Type;
19+
import java.util.function.Function;
20+
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.SpringBootConfiguration;
26+
import org.springframework.boot.test.context.SpringBootTest;
27+
import org.springframework.cloud.function.context.config.FunctionContextUtils;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.support.GenericApplicationContext;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
@SpringBootTest
34+
class TypeResolverHelperIT {
35+
36+
@Autowired
37+
GenericApplicationContext applicationContext;
38+
39+
@ParameterizedTest(name = "{0} : {displayName} ")
40+
@ValueSource(strings = { "weatherClassDefinition", "weatherFunctionDefinition", "standaloneWeatherFunction" })
41+
void beanInputTypeResolutionTest(String beanName) {
42+
assertThat(applicationContext).isNotNull();
43+
Type beanType = FunctionContextUtils.findType(applicationContext.getBeanFactory(), beanName);
44+
assertThat(beanType).isNotNull();
45+
Type functionInputType = TypeResolverHelper.getFunctionArgumentType(beanType, 0);
46+
assertThat(functionInputType).isNotNull();
47+
assertThat(functionInputType.getTypeName()).isEqualTo(WeatherRequest.class.getName());
48+
49+
}
50+
51+
public record WeatherRequest(String city) {
52+
}
53+
54+
public record WeatherResponse(float temperatureInCelsius) {
55+
}
56+
57+
public static class Outer {
58+
59+
public static class InnerWeatherFunction implements Function<WeatherRequest, WeatherResponse> {
60+
61+
@Override
62+
public WeatherResponse apply(WeatherRequest weatherRequest) {
63+
return new WeatherResponse(42.0f);
64+
}
65+
66+
}
67+
68+
}
69+
70+
@SpringBootConfiguration
71+
public static class TypeResolverHelperConfiguration {
72+
73+
@Bean()
74+
Outer.InnerWeatherFunction weatherClassDefinition() {
75+
return new Outer.InnerWeatherFunction();
76+
}
77+
78+
@Bean()
79+
Function<WeatherRequest, WeatherResponse> weatherFunctionDefinition() {
80+
return new Outer.InnerWeatherFunction();
81+
}
82+
83+
@Bean()
84+
StandaloneWeatherFunction standaloneWeatherFunction() {
85+
return new StandaloneWeatherFunction();
86+
}
87+
88+
}
89+
90+
}

0 commit comments

Comments
 (0)