Skip to content

Commit cd0cc46

Browse files
authored
Fix parameter ordering in JsonSchemaGenerator
Incorrect usage of ClassUtils.isAssignable. Incorrectly reversing the parameters causes the method to always return true when an Object is passed. Fixes: #3729 Auto-cherry-pick to 1.0.x Signed-off-by: TheEterna <125226601+TheEterna@users.noreply.github.com>
1 parent 61c32ae commit cd0cc46

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

spring-ai-model/src/main/java/org/springframework/ai/util/json/schema/JsonSchemaGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static String generateForMethodInput(Method method, SchemaOption... schem
129129
String parameterName = method.getParameters()[i].getName();
130130
Type parameterType = method.getGenericParameterTypes()[i];
131131
if (parameterType instanceof Class<?> parameterClass
132-
&& ClassUtils.isAssignable(parameterClass, ToolContext.class)) {
132+
&& ClassUtils.isAssignable(ToolContext.class, parameterClass)) {
133133
// A ToolContext method parameter is not included in the JSON Schema
134134
// generation.
135135
// It's a special type used by Spring AI to pass contextual data to tools

spring-ai-model/src/test/java/org/springframework/ai/util/json/JsonSchemaGeneratorTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,29 @@ void generateSchemaForMethodWithOpenApiSchemaAnnotations() throws Exception {
161161
assertThat(schema).isEqualToIgnoringWhitespace(expectedJsonSchema);
162162
}
163163

164+
@Test
165+
void generateSchemaForMethodWithObjectParam() throws Exception {
166+
Method method = TestMethods.class.getDeclaredMethod("objectParamMethod", Object.class);
167+
168+
String schema = JsonSchemaGenerator.generateForMethodInput(method);
169+
String expectedJsonSchema = """
170+
{
171+
"$schema": "https://json-schema.org/draft/2020-12/schema",
172+
"type": "object",
173+
"properties": {
174+
"object": {
175+
}
176+
},
177+
"required": [
178+
"object"
179+
],
180+
"additionalProperties": false
181+
}
182+
""";
183+
184+
assertThat(schema).isEqualToIgnoringWhitespace(expectedJsonSchema);
185+
}
186+
164187
@Test
165188
void generateSchemaForMethodWithJacksonAnnotations() throws Exception {
166189
Method method = TestMethods.class.getDeclaredMethod("jacksonMethod", String.class, String.class);
@@ -662,6 +685,9 @@ static class TestMethods {
662685
public void simpleMethod(String name, int age) {
663686
}
664687

688+
public void objectParamMethod(Object object) {
689+
}
690+
665691
public void annotatedMethod(
666692
@ToolParam(required = false, description = "The username of the customer") String username,
667693
@ToolParam(required = true) String password) {

0 commit comments

Comments
 (0)