GH-5803: Apply @Schema constraint attributes to @Tool method parameters
Thank you for taking time to contribute this pull request!
Problem
JsonSchemaGenerator.generateForMethodInput() manually reads @Schema on method parameters for description and requiredMode, but silently drops all constraint attributes:
minimum/maximumexclusiveMinimum/exclusiveMaximumminLength/maxLengthpatternexampleallowableValues(mapped toenum)multipleOf
For example, this tool parameter produces no minimum/maximum in the generated schema:
@Tool(name = "set_chance", description = "Set the chance value")
public String setChance(
@Schema(description = "Chance percentage", minimum = "0", maximum = "100")
int chance
) { ... }
Solution
Add a private applySchemaConstraints(Parameter, ObjectNode) method that reads the @Schema annotation on a method parameter and applies its constraint attributes to the generated ObjectNode. This is called once per parameter in the existing generateForMethodInput loop, after the base schema and description have been set.
Numeric constraints (minimum, maximum, multipleOf) are parsed via a helper tryParseDouble() that silently skips non-numeric values to match the lenient nature of the Swagger @Schema annotation.
Changes
JsonSchemaGenerator.java— addedapplySchemaConstraints()+tryParseDouble()helpers; called from the parameter loop ingenerateForMethodInput()JsonSchemaGeneratorTests.java— addedgenerateSchemaForMethodWithOpenApiSchemaConstraintstest coveringminimum/maximum,minLength/maxLength/pattern, andallowableValues; addedschemaConstraintsMethodtoTestMethods
Checklist
- [x] Added
Signed-off-byline to commit (DCO) - [x] Rebased on latest
main - [x] Unit tests added
- [x] Build passes (
mvn package -pl spring-ai-model)
Fixes #5803
LGTM — well-structured implementation. The applySchemaConstraints method is clean, handles all relevant @Schema constraint attributes (minLength, maxLength, pattern, example, allowableValues, etc.), and tryParseDouble handles edge cases gracefully. The test coverage looks solid. This aligns with coding-factory param validation patterns. :ship:
LGTM. Clean, well-scoped fix. The new applySchemaConstraints() helper correctly handles all constraint types (minimum/maximum/exclusiveMinimum/exclusiveMaximum, minLength/maxLength, pattern, example, allowableValues/enum, multipleOf) and is called at the right point in the existing parameter loop. tryParseDouble() for numeric fields is the right approach — lenient parsing avoids breaking on non-numeric values in the annotation. Test covers all new constraint types and uses the standard TestMethods pattern. No risk to existing behavior since constraints are additive-only. Ship it.