Skip to content

feat(codegen): add opt-in aliases for mapped Rust types - #320

Merged
Bergmann89 merged 2 commits into
Bergmann89:masterfrom
sundermann:feat/native-type-aliases
Nov 11, 2026
Merged

Bergmann89 merged 2 commits into
Bergmann89:masterfrom
sundermann:feat/native-type-aliases

Conversation

@sundermann

@sundermann sundermann commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Adds optional aliases that preserve schema names and documentation for types mapped to existing Rust types. This keeps schema types available in the generated public API without introducing wrappers.

Changes

  • Add CustomMeta::with_alias(true) to generate schema-named aliases and reference them in generated fields. Preserve documentation, configured renames, namespace/schema placement, and distinct declarations during deduplication.
  • Lower aliased mappings to the existing ReferenceData / TypedefMode::Typedef representation in the generator. All four type renderers reuse their existing typedef handling, including its treatment of attributes, and custom generator hooks still run.
  • Document the synthetic reference metadata for native aliases, their full-path targets regardless of import settings, and the self:: / crate:: path rules on include_from.
  • Invoke custom generator hooks explicitly for aliased mappings. Include the alias flag in CustomMeta equality and hashing, with regression coverage for mapping identity and hooks running once with aliases enabled or disabled.
  • Keep the IdentPath::relative_to anchor correction in a separate fix(codegen) commit. Add rows to its existing test table for crate::a::B and self::a::B at the generated root and in nested modules. The crate-qualified case fails with an assertion before the fix.
  • Add an XSD fixture and compiled snapshots for the plain, quick-xml, Serde quick-xml, and serde-xml-rs 0.7/0.8 renderers. Cover absolute, crate-qualified, and generated-root-relative includes, aliases disabled, and aliases under global newtype mode. Include quick-xml and all three Serde read/write round trips and native-type identity checks.
  • Match missing-target and name-collision errors by their variants.
  • Remove the README usage section and add the runnable examples/native_aliases crate.

Public API changes: CustomMeta gains the alias field, and GeneratorError gains MissingCustomAliasTarget. Direct CustomMeta initializers and exhaustive generator-error matches must account for these additions. CustomMeta equality and hashing distinguish alias-enabled mappings from direct mappings. Alias generation is disabled by default.

Checklist

  • Tests for the change (a bug fix has a test that fails without the fix)
  • Changed snapshots are regenerated with update-expectations and reviewed
  • cargo fmt, cargo clippy and cargo test pass (see CONTRIBUTING.md)
  • Release examples, including native_aliases, and cargo build -p benchmark --release pass
  • Breaking changes of the public API or the generated code are described above

@Bergmann89 Bergmann89 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sundermann,

Thanks, nice feature. Checks pass locally, and with the default (with_alias(false)) nothing changes in the existing output, good.

Before merging:

  • Tests: tests/feature/native_aliases only compares TokenStream::to_string(), so nothing checks that the generated code compiles, and the quick_xml / serde renderers aren't covered even though the description says all renderers are supported. Please switch to the usual layout (generate_test with compiled expected/default.rs and expected/quick_xml.rs), add a root-relative include, and a quick_xml read/write round trip.
  • IdentPath::relative_to: the crate:: / self:: handling changes the behaviour for all callers (also non-alias includes and trait paths). It's actually a bug fix of its own (before it produced super::crate::...). Please add rows for crate::a::B and self::a::B to the existing relative_to test table, ideally as a separate fix(...) commit.
  • CustomData::render_alias largely repeats the existing TypedefMode::Typedef rendering and is called from four render steps. Could the generator just lower an aliased custom type to the existing typedef path? If the new branch stays, please at least make it match the typedef branch, i.e. without extra_attributes.
  • The error tests should match on the variants (GeneratorError::MissingCustomAliasTarget, DuplicateTypeName) instead of to_string().contains(..).

Documentation

  • Please remove the README section. If you want to show how to use it, an example crate under examples/ is the better place.

@sundermann
sundermann force-pushed the feat/native-type-aliases branch 2 times, most recently from 0c169e0 to 95aa159 Compare November 11, 2026 21:53
Keep crate-qualified paths anchored to the Rust crate and resolve self-qualified paths from the generated root. This prevents invalid paths such as super::crate::a::B when rendering nested modules.
Preserve schema names and documentation when custom mappings opt into aliases. Lower these mappings to existing typedef data so native values keep their identity and all renderers share the normal typedef implementation.

Keep mapped paths and custom generation hooks intact, report missing targets and generated-name collisions, and provide a runnable native-alias example.

Document alias target metadata and path resolution, and distinguish native aliases from direct mappings in custom type equality and hashing.
@sundermann
sundermann force-pushed the feat/native-type-aliases branch from 95aa159 to 9fb5246 Compare November 11, 2026 22:22
@Bergmann89
Bergmann89 merged commit dd86201 into Bergmann89:master Nov 11, 2026
2 checks passed
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.

2 participants