From 3f8486e127b2910460ca65ee9bae19616e433b4e Mon Sep 17 00:00:00 2001 From: iscai-msft <43154838+iscai-msft@users.noreply.github.com> Date: Thu, 15 Feb 2024 13:25:42 -0500 Subject: [PATCH] [tcgc] allow union variant discriminators (#260) --- .../union_discriminator-2024-1-13-16-23-22.md | 7 ++++ .../src/types.ts | 5 +-- .../test/types.test.ts | 34 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 .chronus/changes/union_discriminator-2024-1-13-16-23-22.md diff --git a/.chronus/changes/union_discriminator-2024-1-13-16-23-22.md b/.chronus/changes/union_discriminator-2024-1-13-16-23-22.md new file mode 100644 index 0000000000..e537341ca6 --- /dev/null +++ b/.chronus/changes/union_discriminator-2024-1-13-16-23-22.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +allow models to have a union variant as a discriminator \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index cc735e39f9..e7c8b050f3 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -689,10 +689,7 @@ export function getClientType(context: SdkContext, type: Type, operation?: Opera addFormatInfo(context, type, innerType); return getKnownValuesEnum(context, type, operation) ?? innerType; case "UnionVariant": - return { - ...getSdkTypeBaseHelper(context, type, "any"), - encode: getEncodeHelper(context, type, "any"), - }; + return getClientType(context, type.type, operation); case "EnumMember": const enumType = getSdkEnum(context, type.enum, operation); return getSdkEnumValue(context, enumType, type); diff --git a/packages/typespec-client-generator-core/test/types.test.ts b/packages/typespec-client-generator-core/test/types.test.ts index 06b94016db..5814ecf10b 100644 --- a/packages/typespec-client-generator-core/test/types.test.ts +++ b/packages/typespec-client-generator-core/test/types.test.ts @@ -1488,6 +1488,40 @@ describe("typespec-client-generator-core: types", () => { strictEqual(sharktypeProperty.type.kind, "string"); }); + it("union discriminator", async () => { + await runner.compileWithBuiltInService(` + union KindType { + string, + shark: "shark", + salmon: "salmon" + }; + + @discriminator("kind") + model Fish { + age: int32; + } + + model Shark extends Fish { + kind: KindType.shark; + hasFin: boolean; + } + + model Salmon extends Fish { + kind: KindType.salmon; + norweigan: boolean; + } + + @get + op getModel(): Fish; + `); + const models = Array.from(getAllModels(runner.context)); + strictEqual(models.length, 3); + const shark = models.find((x) => x.name === "Shark")! as SdkModelType; + strictEqual(shark.discriminatorValue, "shark"); + const salmon = models.find((x) => x.name === "Salmon")! as SdkModelType; + strictEqual(salmon.discriminatorValue, "salmon"); + }); + it("filterOutCoreModels true", async () => { const runnerWithCore = await createSdkTestRunner({ librariesToAdd: [AzureCoreTestLibrary],