[http-client-csharp] Preserve enum type when @alternateType collapses single-member enum to constant#10281
[http-client-csharp] Preserve enum type when @alternateType collapses single-member enum to constant#10281
Conversation
…num to constant Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/f1259764-9aff-4b82-87b9-341ba62673a2 Co-authored-by: live1206 <5196139+live1206@users.noreply.github.com>
…iew feedback Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/f1259764-9aff-4b82-87b9-341ba62673a2 Co-authored-by: live1206 <5196139+live1206@users.noreply.github.com>
commit: |
|
No changes needing a change description found. |
| } | ||
|
|
||
| model TestModel { | ||
| @alternateType("green") |
There was a problem hiding this comment.
Is there ever a case where we would want prop to be a constant and not an extensible enum? Also, ideally, we would validate this doesn't cause any change in behavior for existing libraries. I am not aware of any that do this, but there might be. @JoshLove-msft - any thoughts on this behavioral change?
|
enum Colors { green } model EmployeeProperties { To me, this does not really make sense. if we really want extensible enum, we should do this: FYI - TCGC emits fixed enum for "closed unions" like |
When
@alternateType("green")is applied to a required property of typeColors(a single-member enum), TCGC resolves it as anSdkConstantTypeinstead ofSdkEnumType. The emitter'scase "constant":branch only promoted constants to extensible enums for optional/nullable properties, so required properties collapsed to string literals.Changes
emitter/src/lib/type-converter.ts: In thecase "constant":branch, checksdkProperty.__raw?.type?.kind === "Enum"to detect when the original TypeSpec property type was an enum. When true, convert to extensible enum regardless of optionality.emitter/test/Unit/constant-type.test.ts: Added test for single-member enum with@alternateTypeon a required property, assertingkind === "enum"andisFixed === false.Repro
Before:
favoriteColoremitted as constant"green"After:
favoriteColoremitted as extensible enum type