spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

GH-5803: Apply @Schema constraint attributes to @Tool method parameters

Open suryateja-g13 opened this issue 7 months ago • 2 comments

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 / maximum
  • exclusiveMinimum / exclusiveMaximum
  • minLength / maxLength
  • pattern
  • example
  • allowableValues (mapped to enum)
  • 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 — added applySchemaConstraints() + tryParseDouble() helpers; called from the parameter loop in generateForMethodInput()
  • JsonSchemaGeneratorTests.java — added generateSchemaForMethodWithOpenApiSchemaConstraints test covering minimum/maximum, minLength/maxLength/pattern, and allowableValues; added schemaConstraintsMethod to TestMethods

Checklist

  • [x] Added Signed-off-by line to commit (DCO)
  • [x] Rebased on latest main
  • [x] Unit tests added
  • [x] Build passes (mvn package -pl spring-ai-model)

Fixes #5803

suryateja-g13 avatar Apr 21 '26 13:04 suryateja-g13

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:

anuragg-saxenaa avatar Apr 22 '26 04:04 anuragg-saxenaa

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.

anuragg-saxenaa avatar Apr 22 '26 11:04 anuragg-saxenaa