GH-5803: Apply @Schema constraint attributes to @Tool method parameters#5848
GH-5803: Apply @Schema constraint attributes to @Tool method parameters#5848suryateja-g13 wants to merge 1 commit intospring-projects:mainfrom
Conversation
…method parameters generateForMethodInput() read @Schema on method parameters for 'description' and 'requiredMode' but silently dropped constraint attributes: minimum, maximum, exclusiveMinimum, exclusiveMaximum, minLength, maxLength, pattern, example, allowableValues, and multipleOf. Add applySchemaConstraints() to map these attributes onto the generated ObjectNode for each parameter after the base schema is produced. Fixes spring-projectsGH-5803 (spring-projects#5803) Signed-off-by: Gorre Surya <sgorre92@gmail.com>
|
LGTM — well-structured implementation. The |
|
LGTM. Clean, well-scoped fix. The new |
redinside-dev
left a comment
There was a problem hiding this comment.
LGTM. Clean fix that adds applySchemaConstraints() + tryParseDouble() helpers to bridge the gap between @Schema annotation constraint attributes and the generated JSON schema. The test coverage is solid - minimum/maximum, minLength/maxLength/pattern, and allowableValues/enum all verified. Two small notes for future improvement: (1) could extract the allowableValues/enum logic into a dedicated method since it builds an array, and (2) @Schema(allowableValues) currently only handles String enums - if someone passes e.g. allowableValues = {"1", "2"} for an int parameter, it will generate String enum values instead of integers, which could be confusing. Neither is a blocker for merging.
Testing: build passes (mvn package -pl spring-ai-model). Ship it.
Thank you for taking time to contribute this pull request!
Problem
JsonSchemaGenerator.generateForMethodInput()manually reads@Schemaon method parameters fordescriptionandrequiredMode, but silently drops all constraint attributes:minimum/maximumexclusiveMinimum/exclusiveMaximumminLength/maxLengthpatternexampleallowableValues(mapped toenum)multipleOfFor example, this tool parameter produces no
minimum/maximumin the generated schema:Solution
Add a private
applySchemaConstraints(Parameter, ObjectNode)method that reads the@Schemaannotation on a method parameter and applies its constraint attributes to the generatedObjectNode. This is called once per parameter in the existinggenerateForMethodInputloop, after the base schema and description have been set.Numeric constraints (
minimum,maximum,multipleOf) are parsed via a helpertryParseDouble()that silently skips non-numeric values to match the lenient nature of the Swagger@Schemaannotation.Changes
JsonSchemaGenerator.java— addedapplySchemaConstraints()+tryParseDouble()helpers; called from the parameter loop ingenerateForMethodInput()JsonSchemaGeneratorTests.java— addedgenerateSchemaForMethodWithOpenApiSchemaConstraintstest coveringminimum/maximum,minLength/maxLength/pattern, andallowableValues; addedschemaConstraintsMethodtoTestMethodsChecklist
Signed-off-byline to commit (DCO)mainmvn package -pl spring-ai-model)Fixes #5803