Skip to content

Generate nullable JSON Schema types for @Nullable members - #7004

Open
Hugo-DDT wants to merge 1 commit into
spring-projects:mainfrom
Hugo-DDT:fix-json-schema-nullable-type
Open

Hugo-DDT wants to merge 1 commit into
spring-projects:mainfrom
Hugo-DDT:fix-json-schema-nullable-type

Conversation

@Hugo-DDT

Copy link
Copy Markdown

What changed

Registered a withNullableCheck in AbstractSpringAiSchemaModule so that members detected as nullable (via @Nullable annotations or Kotlin nullability, using Spring's Nullness detection) are typed to also admit null in the generated JSON Schema.

For a @Nullable String field, the schema previously emitted:

"email" : { "type" : "string" }

while also dropping email from required — producing a schema with no valid value for that property. It now emits:

"email" : { "type" : ["string", "null"] }

Why

checkRequired already treats @Nullable/Kotlin-nullable members as non-required, but nothing registered the corresponding withNullableCheck, so the generated schema was unsatisfiable for nullable properties. This mirrors the Nullness detection already used by checkRequired, and follows the KotlinModule precedent for withNullableCheck.

How verified

Updated the existing pinning test generateSchemaForTypeWithJSpecifyNullableField to assert the nullable email field is typed as ["string", "null"] while the non-nullable name field remains "string" and email stays out of required.

Related

Relates to #6950 (addresses the @Nullable half; the question of whether unannotated reference types should be nullable-by-default is left for a separate discussion).

Signed-off-by: DDT <1786035110@qq.com>
@Hugo-DDT
Hugo-DDT force-pushed the fix-json-schema-nullable-type branch from 2cafe2d to 099ddb5 Compare September 16, 2026 16:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Nullable schema handling is covered by the updated regression test, with no unresolved blocking issues.

Pull request overview

Updates JSON Schema generation so nullable members accept null while remaining optional.

Changes:

  • Registers Spring Nullness-based nullable schema handling.
  • Updates the nullable-field regression test.
File summaries
File Description
spring-ai-model/src/test/java/org/springframework/ai/util/json/JsonSchemaGeneratorTests.java Verifies nullable fields use ["string", "null"].
spring-ai-model/src/main/java/org/springframework/ai/util/json/schema/AbstractSpringAiSchemaModule.java Adds nullable type detection.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

3 participants