fix(generator): treat string const as single-value enum (closes #10)#12
Merged
Conversation
A property like `{ "type": "string", "const": "X" }` previously generated
`Option<String>`, leaving the generated client able to send any string
value. Treat it as a degenerate single-value enum so the generator emits
a tightly-typed single-variant enum, matching how `enum: ["X"]` is
already handled.
Centralized in `SchemaDetails::is_string_enum` / `string_enum_values` so
all four call sites (typed-string, untyped-string, property-with-context,
inferred-string) pick up the change consistently.
Discriminator paths are unaffected: `extract_inline_discriminator_value`
reads `const_value` directly, and the variant struct's discriminator
field is filtered out of the generated struct (`generator.rs:932-947`).
Note: the existing orphan-enum pattern (e.g. `DogSpecies` in
`discriminator_no_mapping.snap`) becomes more visible because more
discriminator fields now produce single-variant enums. Tracked
separately in #11.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Generated code for properties with a string `const` (no `enum` array) changes from `Option<String>` to `Option<<Struct><Prop>>` enum, which is a breaking change for downstream consumers constructing those types. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
5 tasks
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.
Summary
{ "type": "string", "const": "X" }(noenumarray) as a degenerate single-value enum so the generator emits a tightly-typed single-variant enum instead ofOption<String>.SchemaDetails::is_string_enum/string_enum_valuesso all four call sites (typed-string, untyped-string, property-with-context, inferred-string) pick up the change.Before / After
For the spec from #10:
Before:
After:
Construction:
Discriminator paths are unaffected
extract_inline_discriminator_value(src/analysis.rs:2507) readsconst_valuedirectly when picking variant tags.src/generator.rs:932-947, so this change does not affect (de)serialization of tagged unions.Snapshot drift
Three pre-existing snapshots are updated, all in the desired direction (bare
String→ single-variant enum on standalone structs):debug_test.snaptype_property_only_test.snaponeof_in_property.snap(ImageBlock.typefield —ImageBlockitself is not a discriminated-union variant)array_union_items.snapThe
oneof_in_propertyandarray_union_itemssnapshots also pick up some orphan single-variant enums (e.g.,URLImageSourceType,Base64ImageSourceType) for discriminator fields whose enums are no longer referenced by their parent struct. This is a pre-existing behavior — visible today indiscriminator_no_mapping.snap(DogSpecies/CatSpecies) for anyenum: ["X"]discriminator field. Tracked separately in #11; intentionally out of scope here.Test plan
tests/const_only_property_test.rsreproducing Overly Broad Type Generated When Using"const"#10 (both required and optional cases) — both pass.cargo testsuite — all green.cargo fmt— clean.🤖 Generated with Claude Code