Skip to content

breaking: Model tagged enum variants as SchemaType::Tagged - #224

Merged
milesj merged 2 commits into
masterfrom
0.22-tagged-type
Nov 17, 2026
Merged

milesj merged 2 commits into
masterfrom
0.22-tagged-type

Conversation

@milesj

@milesj milesj commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Summary

A tagged enum variant's schema is now a SchemaType::Tagged. That is a TaggedType holding:

  • its TagFormat (External, Internal { tag }, or Adjacent { tag, content })
  • the variant's name
  • the tag's env_var
  • its value, untouched

Before 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_schema and the Pkl renderer treated any literal field as the tag.
  • The derive invented a field key for an internally tagged named struct, just to flatten it.
  • Writing onto a named type's shared schema leaked tags and descriptions into every type that holds it.

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:

  • 61d215c fixes proto's generated TypeScript, where SourceLocation had become a union of object literals instead of ArchiveSource | GitSource references. 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.
  • bb05199 adds the model described above.

Changes

  • types
    • New TaggedType / TagFormat (crates/types/src/tagged.rs) and Schema::tagged.
    • TaggedType::to_schema() returns the struct serde writes, which replaces the hidden internal::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.
  • derive (core): emits Schema::tagged(TaggedType::new(..)) for every tagged variant, with the tag's env var.
  • runtime
    • The generator walks into a variant's value, so a type that only a variant holds is still registered.
    • partialize_schema only reaches the value, and no longer treats a literal field as a tag.
  • renderers
    • SchemaRenderer::render_tagged defaults to rendering the struct from to_schema, so a custom renderer that only renders structs keeps its output.
    • TypeScript and JSON Schema render through that default. TypeScript's is_intersection looks through a Tagged, so a union member keeps its parentheses.
    • Pkl, API docs, and the templates read struct shapes in many places, so they call expand_tagged on every schema when they start rendering.
  • docs: a new book page (book/src/schema/tagged.md), a custom renderer note, AGENTS.md gotchas, and the changelog.

Breaking

  • SchemaType gains a variant and isn't #[non_exhaustive], so exhaustive matches downstream stop compiling.
  • Code that reads a union's variants finds a Tagged rather than a struct.
  • Serialized schemas (schema_serde) write a tagged variant as { "type": "Tagged", ... }.
  • A literal setting in a partial is now optional like any other.

This makes it a 0.22 / schematic_types 0.14 change.

Reviewer notes

  • Rendered output is unchanged. Every TypeScript, JSON Schema, Pkl, template, and API docs snapshot is identical. Proto's api-types.ts and registry/schema.json, regenerated against this branch, are byte-identical to the output of 61d215c, and the generated TypeScript passes tsc --strict.
  • Snapshots: the changed core codegen snapshots and the partialize_test model snapshots were each reviewed.
  • Pkl still treats a literal property as a tag, since it sees the expanded struct. Shaping Pkl, or any other renderer, per format is possible through render_tagged, but out of scope here.

Testing

  • cargo test --workspace (1322 passed) and cargo test -p schematic_types (113 passed).
  • New tests:
    • crates/types/tests/schema_test.rs (tagged) covers each format, partialize, expansion, and a serde round-trip.
    • crates/schematic/tests/tagged_test.rs covers the derive model, generator registration, partials, and the default render_tagged.
  • cargo fmt --all --check passes.
  • cargo clippy --workspace --all-targets reports only the 5 collapsible_if warnings master already has.
  • Feature builds for schematic (default, none, config, schema, each renderer), schematic_types (with and without serde), and schematic_core (with and without schema). The only remaining warning, the unused json-strip-comments with renderer_json_schema alone, is also on master.

🤖 Generated with Claude Code

milesj and others added 2 commits November 16, 2026 15:43
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>
@milesj
milesj merged commit db61047 into master Nov 17, 2026
6 checks passed
@milesj
milesj deleted the 0.22-tagged-type branch November 17, 2026 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant