Repository navigation
[TCGC] Convert inline discriminated unions to polymorphic models by default #5743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Chenjie Shi (tadelesh)
wants to merge
7
commits into
Azure:main
Choose a base branch
from
tadelesh:feat/convert-discriminated-unions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04300b7
feat(tcgc): convert inline discriminated unions to model hierarchies
d588153
Address discriminated union conversion review feedback
0dc4b0c
Simplify discriminated union conversion to direct base aliases
fe5dc23
Fix discriminated union metadata and emitter review regressions
966d79e
fix(tcgc): exclude unions containing the base from conversion
233dfe4
revert: remove TypeScript emitter changes from TCGC union work
f2d92d9
fix(tcgc): address diagnostic and inheritance review comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.chronus/changes/tcgc-inline-discriminated-union-conversion.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| changeKind: feature | ||
| packages: | ||
| - "@azure-tools/typespec-client-generator-core" | ||
| --- | ||
|
|
||
| Convert inline-discriminated unions with explicit nominal model hierarchies to polymorphic SDK models by default. Set `convertDiscriminatedUnionsToModels: false` in `createSdkContext` to preserve union representations. Ordinary unions and object-envelope unions retain their existing behavior. |
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
72 changes: 72 additions & 0 deletions
72
...pespec-client-generator-core/src/diagnostics/union-model-conversion-conflict.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| This diagnostic is issued when an inline-discriminated union's discriminator property or tag mappings conflict with another union or an existing model discriminator in the same hierarchy. | ||
|
|
||
| ## Impact | ||
|
|
||
| - **Area:** SDK union type conversion. Generation continues with the union representation instead of overwriting conflicting discriminator metadata. | ||
| - **Not affected:** The TypeSpec union declaration, service routes, and wire payloads are unchanged. | ||
|
|
||
| ## ❌ Incorrect Usage | ||
|
|
||
| ```typespec | ||
| @service | ||
| namespace Example; | ||
|
|
||
| model PetBase { | ||
| name: string; | ||
| } | ||
|
|
||
| model Cat extends PetBase { | ||
| meows: boolean; | ||
| } | ||
|
|
||
| @discriminated(#{ envelope: "none" }) | ||
| union Pet extends PetBase { | ||
| cat: Cat, | ||
| feline: Cat, // one model cannot have two polymorphic discriminator values | ||
| } | ||
|
|
||
| op get(): Pet; | ||
| ``` | ||
|
|
||
| ## Diagnostic Message | ||
|
|
||
| TCGC reports: | ||
|
|
||
| ```text | ||
| Cannot convert discriminated union 'Pet' to a model hierarchy because its discriminator metadata conflicts with another union or model using the same hierarchy. The union is preserved. Set convertDiscriminatedUnionsToModels to false to retain union representations. | ||
| ``` | ||
|
|
||
| ## ✅ How to Fix | ||
|
|
||
| Use consistent discriminator properties and tag mappings throughout the shared hierarchy. Give each alternative with a different wire tag its own model. | ||
|
|
||
| ```typespec | ||
| @service | ||
| namespace Example; | ||
|
|
||
| model PetBase { | ||
| name: string; | ||
| } | ||
|
|
||
| model Cat extends PetBase { | ||
| meows: boolean; | ||
| } | ||
|
|
||
| model Dog extends PetBase { | ||
| barks: boolean; | ||
| } | ||
|
|
||
| @discriminated(#{ envelope: "none" }) | ||
| union Pet extends PetBase { | ||
| cat: Cat, | ||
| dog: Dog, | ||
| } | ||
|
|
||
| op get(): Pet; | ||
| ``` | ||
|
|
||
| Emitters that intentionally use native discriminated unions can set `convertDiscriminatedUnionsToModels: false` in `createSdkContext` instead of requesting polymorphic conversion. | ||
|
|
||
| ## Suppression | ||
|
|
||
| This diagnostic should not be suppressed. Align the hierarchy's discriminator metadata, or disable conversion when the emitter intentionally retains discriminated unions. |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.