Skip to content

fix(java): emit @JsonFormat for optional date/time properties#3049

Merged
schani merged 2 commits into
masterfrom
agent/fix-issue-1593
Jul 21, 2026
Merged

fix(java): emit @JsonFormat for optional date/time properties#3049
schani merged 2 commits into
masterfrom
agent/fix-issue-1593

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

Bug

With --datetime-provider legacy, the Java (Jackson) renderer attaches
@JsonFormat(pattern = ...) to date/date-time/time properties that
are required, but silently omits it for the same kind of properties
when they are optional — producing inconsistent serialization/parsing
behavior between the two, even though both hold the same value type.

Repro:

node dist/index.js --lang java --src-lang schema --lombok --datetime-provider legacy demo.schema

with a schema containing both a required and an optional date-time
property. Before the fix, only the required property got @JsonFormat;
after the fix, both do.

Root cause

JavaJacksonRenderer.annotationsForAccessor switched directly on
p.type.kind to decide which @JsonFormat annotation to emit. A
required date/date-time/time property has that kind directly on its
type, so the switch matched. An optional property is represented
internally as a nullable union (date-time | null), so p.type.kind is
"union" and none of the cases matched, silently skipping the
annotation. This was invisible with the default java8 datetime
provider because that provider's annotation lists are empty regardless.

Fix

Unwrap a nullable union to its non-null member before switching on the
kind, using the same nullableFromUnion pattern already used elsewhere
in the Java renderer (e.g. JavaRenderer.javaType). Minimal, four-line
change in packages/quicktype-core/src/language/Java/JavaJacksonRenderer.ts.

Test coverage

Added test/inputs/schema/optional-date-time.schema (+
.1.json sample) with matching required and optional date, time, and
date-time properties. This schema is picked up automatically by the
existing schema-java-datetime-legacy fixture (JavaLanguageWithLegacyDateTime
in test/languages.ts, registered in test/fixtures.ts), and is not
excluded by that language's skipSchema list.

Verification done locally

  • npm run build passes.
  • Re-ran the original repro: optionalDate/requiredDate now get
    identical @JsonFormat annotations under --datetime-provider legacy.
  • Confirmed the default java8 provider still emits zero @JsonFormat
    annotations (unaffected/expected behavior).
  • Spot-checked code generation for the new schema file across several
    other languages (C#, Python, Go, Rust, Swift, Kotlin, TypeScript) to
    confirm the new fixture input doesn't break unrelated languages.
  • Maven is not available in this sandbox, so the full compile-and-run
    fixture (FIXTURE=schema-java-datetime-legacy script/test) could not
    be executed end-to-end locally; CI will run it.

Fixes #1593

🤖 Generated with Claude Code

schani and others added 2 commits July 20, 2026 19:07
With --datetime-provider legacy, JavaJacksonRenderer switched on the raw
property type kind to decide whether to add @jsonformat. Required
date/date-time/time properties have that kind directly, but optional
properties are represented as a nullable union, so the switch fell
through and the annotation was silently omitted, producing inconsistent
serialization between required and optional fields of the same format.

Unwrap a nullable union to its non-null member (matching the pattern
already used in JavaRenderer.javaType) before switching on the kind.

Added test/inputs/schema/optional-date-time.schema (+ .1.json sample)
with matching required/optional date, time, and date-time properties,
exercised by the existing schema-java-datetime-legacy fixture.

Fixes #1593

Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
Co-Authored-By: Claude <noreply@anthropic.com>
@schani
schani merged commit 58dad07 into master Jul 21, 2026
28 checks passed
@schani
schani deleted the agent/fix-issue-1593 branch July 21, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]About date format in java.

1 participant