Skip to content

Use Jackson enum values in generated schemas#6646

Open
ChoMinGi wants to merge 1 commit into
spring-projects:mainfrom
ChoMinGi:GH-1985
Open

Use Jackson enum values in generated schemas#6646
ChoMinGi wants to merge 1 commit into
spring-projects:mainfrom
ChoMinGi:GH-1985

Conversation

@ChoMinGi

Copy link
Copy Markdown
Contributor

Fixes #1985

Jackson uses the values declared by @JsonValue or @JsonProperty when serializing and deserializing enums, but generated JSON schemas currently advertise the Java constant names. A model following such a schema can therefore produce a value that BeanOutputConverter cannot deserialize.

This change enables victools' FLATTENED_ENUMS_FROM_JSONVALUE and FLATTENED_ENUMS_FROM_JSONPROPERTY options in JsonSchemaGenerator, JsonSchemaUtils, and McpJsonSchemaGenerator. This keeps structured output, tool input, and MCP schemas consistent with Jackson's enum representation.

The @JsonProperty mapping applies when all constants are annotated. Plain enums keep their existing constant names. For the affected enums, the previous schema advertised values the default deserializer rejects, so enabling the options by default corrects broken behavior rather than changing a working setup. No public API or converter-selection behavior is changed.

The option pair was originally suggested in the #1985 discussion; the general schema-generator customization hook raised in the same thread is left as a separate follow-up.

An upgrade note and regression tests are included.

Tests

  • JsonSchemaGeneratorTests
  • JsonSchemaGeneratorKotlinTests — victools resolves @JsonValue from methods, so Kotlin properties use the @get: site target
  • JsonSchemaUtilsTests
  • BeanOutputConverterTest
  • McpJsonSchemaGeneratorTests
./mvnw -pl spring-ai-model,spring-ai-client-chat,mcp/mcp-annotations test

Jackson serializes enums using their `@JsonValue` method or the
`@JsonProperty` values on constants, but generated JSON schemas
advertised the Java constant names. A model following the schema then
produces values the deserializer rejects with InvalidFormatException.

Enable FLATTENED_ENUMS_FROM_JSONVALUE and
FLATTENED_ENUMS_FROM_JSONPROPERTY in the victools Jackson module of
JsonSchemaGenerator, JsonSchemaUtils, and McpJsonSchemaGenerator so
schema enum values match Jackson's serialized form. Both options only
affect enums using those annotations; plain enums keep emitting their
constant names.

Fixes spring-projects#1985

Signed-off-by: Mingi Cho <81455273+ChoMinGi@users.noreply.github.com>
@ChoMinGi ChoMinGi changed the title Honor Jackson enum values in generated schemas Use Jackson enum values in generated schemas Jul 19, 2026
@ChoMinGi
ChoMinGi marked this pull request as ready for review July 19, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for BeanOutputConverter with non-standard enum mapping

2 participants