fix(types): emit working serde codecs for time::Date / time::Time (#25)#26
Merged
Merged
Conversation
DateStrategy::Time mapped `format: date` and `format: time` to `#[serde(with = "time::serde::iso8601")]`, but that module only supports OffsetDateTime — the generated code failed to compile (#25). The generator now declares per-type codec modules in the generated file via `time::serde::format_description!`: - `time_date_format`: "[year]-[month]-[day]" (RFC 3339 full-date) - `time_time_format`: "[hour]:[minute]:[second][optional [.[subsecond]]]" (RFC 3339 partial-time; parses inputs with or without fractional seconds, serializes whole seconds with a trailing ".0") Both come with an `::option` submodule, matching the existing Option-field codec convention. Emission is gated on new TypeFeature::TimeDate / TimeTime so specs without date/time fields stay clean. Two adjacent latent bugs fixed along the way: - REQUIRED_DEPS listed `time = { features = ["serde"] }`, which doesn't even enable the rfc3339 codec; the time line now carries formatting/parsing (plus macros when date/time codecs are used), and collect_dep_requirements unions feature lists instead of dropping duplicates by crate name. - Optional fields with a `with` codec (including format: byte) failed to deserialize when the field was absent — serde's missing-field → None shortcut is disabled by `with` (serde-rs/serde#2878). Such fields now also get #[serde(default)]. Verified end-to-end: generated the issue's exact shape (optional format: date), compiled it against time 0.3, and round-tripped present/absent/fractional values. Closes #25 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #25.
Problem
With
DateStrategy::Time,format: date/format: timefields were emitted asbut
time::serde::iso8601only supportstime::OffsetDateTime, so the generated code failed to compile (E0308, as reported).Fix
The generated file now declares real codecs for these types via
time::serde::format_description!, gated on actual usage (newTypeFeature::TimeDate/TimeTime, mirroring howbase64_serdeis gated):Fields then use
with = "time_date_format"/"time_date_format::option"etc. The time format parses partial-time with or without fractional seconds; whole seconds serialize with a trailing.0(valid RFC 3339, unavoidable since[optional]groups always format their contents).Two latent bugs surfaced while verifying
timewas wrong even before this:features = ["serde"]doesn't enabletime::serde::rfc3339(needsformatting/parsing). The emitted line is nowtime = { version = "0.3", features = ["serde", "formatting", "parsing", "macros"] }(macrosonly pulled in when date/time codecs are used), andcollect_dep_requirementsunions feature lists for the same crate instead of dropping duplicates.withcodec failed to deserialize ("missing field") — awithattribute disables serde's implicit missing-field →Nonehandling (Optionfields require explicitdefaultattribute ifwithattribute specified serde-rs/serde#2878). This also affected optionalformat: bytefields. Option-wrapped codec fields now also get#[serde(default)].Testing
tests/typed_scalars_test.rscovering required/optional date, time, date-time (still rfc3339, no helper emitted), and the merged dep line.cargo testsuite passes.format: date), compiled it in a scratch crate againsttime0.3.53, and round-tripped present, absent, and fractional-second values successfully.scripts/spec-compile.shsmoke (anthropic/arcade/asana): all compile cleanly.Side note: while testing I found that the README's documented
[generator.types]TOML block is silently ignored — the parser reads top-level[types]. Filed as a separate beads issue (openapi-generator-cwd) since it's independent of this fix.🤖 Generated with Claude Code