Fix handling of DU members that are also DUs#901
Merged
Conversation
Avoid spurious import statement for DU types when in unions.rs. Added Spector coverage for the impacted scenario.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes Rust emitter support for discriminated unions that contain other discriminated unions, and adds a new Spector test crate (“hierarchy-building”) to validate the behavior end-to-end.
Changes:
- Update TCGC adapter + codemodel types to allow discriminated-union members to be either a
Modelor anotherDiscriminatedUnion. - Update union codegen to avoid generating invalid/self-conflicting
usestatements when a DU contains another DU. - Add new Spector test crate + integration tests for hierarchy-building, and bump
@azure-tools/typespec-rustto0.37.1with changelog entry.
Reviewed changes
Copilot reviewed 10 out of 24 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/typespec-rust/src/tcgcadapter/adapter.ts | Emits DU members as nested DUs when subtypes are themselves polymorphic. |
| packages/typespec-rust/src/codemodel/types.ts | Broadens DU member type to DiscriminatedUnion | Model. |
| packages/typespec-rust/src/codegen/unions.ts | Skips adding use for DU members to prevent self-import conflicts in unions.rs. |
| packages/typespec-rust/package.json | Version bump to 0.37.1. |
| packages/typespec-rust/CHANGELOG.md | Adds 0.37.1 (unreleased) entry describing the DU-nesting fix. |
| packages/typespec-rust/.scripts/tspcompile.js | Registers new Spector input spector_hierarchy_building. |
| packages/typespec-rust/test/Cargo.toml | Adds hierarchy-building crate to the test workspace. |
| packages/typespec-rust/test/Cargo.lock | Adds lock entry for spector_hierarchy_building. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/Cargo.toml | New Spector crate manifest for hierarchy-building scenario. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/lib.rs | New crate entrypoint re-exporting generated code. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/mod.rs | Generated module root for clients/models. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/clients/mod.rs | Generated clients module exports. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/clients/hierarchy_building_client.rs | Generated top-level client + subclient accessors. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/clients/hierarchy_building_animal_operations_client.rs | Generated Animal operations client (PUT endpoints). |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/clients/hierarchy_building_dog_operations_client.rs | Generated Dog operations client (PUT endpoint). |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/clients/hierarchy_building_pet_operations_client.rs | Generated Pet operations client (PUT endpoints). |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/models/mod.rs | Generated models module wiring. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/models/method_options.rs | Generated method option structs for hierarchy-building operations. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/models/models.rs | Generated Dog model. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/models/models_impl.rs | Generated conversions + request-content helpers for models. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/models/unions.rs | Generated discriminated unions (Animal, Pet) including nested DU membership. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/models/unions_impl.rs | Generated request-content helpers for unions. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/src/generated/models/unions_serde.rs | Generated custom Serialize for extensible polymorphic unions. |
| packages/typespec-rust/test/spector/azure/client-generator-core/hierarchy-building/tests/hierarchy_building_tests.rs | New integration tests covering DU nesting + request/response roundtrips. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
RickWinter
approved these changes
Mar 13, 2026
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.
Avoid spurious import statement for DU types when in unions.rs. Added Spector coverage for the impacted scenario.