Repository navigation
breaking: Model tagged enum variants as SchemaType::Tagged - #224
Merged
Merged
Conversation
An internally tagged variant holding a named struct was given that struct's
fields plus the tag, without its name, so renderers inlined it, and
TypeScript rendered an object literal per variant rather than
`ArchiveSource | GitSource`. The variant is now a struct of the tag and the
value as a flattened field, so the struct is referenced and defined once,
without the tag: `({ type: 'git' } & GitSource) | ...`. The value is marked
as a partial rather than the struct around it, so a partial references
`PartialChild`.
- TypeScriptRenderer intersects a flattened field of a struct rendered in
place, which it left out, and groups intersections within unions and
arrays.
- ApiDocsRenderer does the same, rather than rendering it as a key.
- An adjacently or externally tagged variant keeps the description of the
type it holds on that type, rather than taking it onto the field.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A tagged variant's schema is now a `TaggedType`, which holds its `TagFormat` (external, internal, or adjacent), its name, the tag's env var, and its value, untouched. The derive no longer builds a struct around the value, so a tag or docs written onto a named type's shared schema can't leak into every type that holds it, and `partialize_schema` no longer treats a literal field as the tag. `TaggedType::to_schema` is the struct serde writes, which replaces the `internal::schema` tag helpers, and `SchemaRenderer::render_tagged` renders it by default. Pkl, API docs, and the templates expand every variant up front with `Schema::expand_tagged`, as they read struct shapes. Rendered output is unchanged. Co-Authored-By: Claude Opus 5.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.
Summary
A tagged enum variant's schema is now a
SchemaType::Tagged. That is aTaggedTypeholding:TagFormat(External,Internal { tag }, orAdjacent { tag, content })env_varBefore this, the derive turned each format into a plain struct. It was either a struct of the tag and the value, or the value with the tag added, and code further down had to guess the tag back:
partialize_schemaand the Pkl renderer treated any literal field as the tag.Keeping the format in the model removes that guessing at the source. An externally tagged unit variant is still a
Literal, and an untagged variant is still its value alone.This PR has two commits:
SourceLocationhad become a union of object literals instead ofArchiveSource | GitSourcereferences. An internally tagged variant holding a named struct now renders as({ type: 'git' } & GitSource) | …. Flattened fields of inline structs are intersected in TypeScript and API docs. Adjacent and external tagging no longer take the held type's description.Changes
TaggedType/TagFormat(crates/types/src/tagged.rs) andSchema::tagged.TaggedType::to_schema()returns the struct serde writes, which replaces the hiddeninternal::schema::{tagged_unit, internally_tagged, adjacently_tagged}helpers.Schema::expand_tagged()replaces every tagged variant within a schema with that struct.Schema::partialize()only marks the value.Schema::tagged(TaggedType::new(..))for every tagged variant, with the tag's env var.partialize_schemaonly reaches the value, and no longer treats a literal field as a tag.SchemaRenderer::render_taggeddefaults to rendering the struct fromto_schema, so a custom renderer that only renders structs keeps its output.is_intersectionlooks through aTagged, so a union member keeps its parentheses.expand_taggedon every schema when they start rendering.book/src/schema/tagged.md), a custom renderer note, AGENTS.md gotchas, and the changelog.Breaking
SchemaTypegains a variant and isn't#[non_exhaustive], so exhaustive matches downstream stop compiling.Taggedrather than a struct.schema_serde) write a tagged variant as{ "type": "Tagged", ... }.This makes it a 0.22 /
schematic_types0.14 change.Reviewer notes
api-types.tsandregistry/schema.json, regenerated against this branch, are byte-identical to the output of 61d215c, and the generated TypeScript passestsc --strict.partialize_testmodel snapshots were each reviewed.render_tagged, but out of scope here.Testing
cargo test --workspace(1322 passed) andcargo test -p schematic_types(113 passed).crates/types/tests/schema_test.rs(tagged) covers each format, partialize, expansion, and a serde round-trip.crates/schematic/tests/tagged_test.rscovers the derive model, generator registration, partials, and the defaultrender_tagged.cargo fmt --all --checkpasses.cargo clippy --workspace --all-targetsreports only the 5collapsible_ifwarnings master already has.schematic(default, none, config, schema, each renderer),schematic_types(with and withoutserde), andschematic_core(with and withoutschema). The only remaining warning, the unusedjson-strip-commentswithrenderer_json_schemaalone, is also on master.🤖 Generated with Claude Code