From 49f8da29234e869bc85e36a33d32950bd327e1c8 Mon Sep 17 00:00:00 2001 From: Mingzhe Huang Date: Tue, 23 Jul 2024 17:07:25 +0800 Subject: [PATCH 1/5] feat(tcgc): throw error when trying to flattening a model with polymorphsim resolve #959 --- ...polymorphism-flatten-2024-6-23-22-12-38.md | 7 +++++ .../src/decorators.ts | 8 ++++++ .../typespec-client-generator-core/src/lib.ts | 6 ++++ .../test/decorators.test.ts | 28 +++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 .chronus/changes/tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md diff --git a/.chronus/changes/tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md b/.chronus/changes/tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md new file mode 100644 index 0000000000..c2cc2aa113 --- /dev/null +++ b/.chronus/changes/tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +throw error when trying to flattening a model with polymorphsim \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/decorators.ts b/packages/typespec-client-generator-core/src/decorators.ts index 4735775550..ab5152a762 100644 --- a/packages/typespec-client-generator-core/src/decorators.ts +++ b/packages/typespec-client-generator-core/src/decorators.ts @@ -17,6 +17,7 @@ import { Type, Union, createDiagnosticCollector, + getDiscriminator, getNamespaceFullName, getProjectedName, ignoreDiagnostics, @@ -953,6 +954,13 @@ export function $flattenProperty( target: ModelProperty, scope?: LanguageScopes ) { + if (getDiscriminator(context.program, target.type)) { + reportDiagnostic(context.program, { + code: "flatten-polymorphism", + format: {}, + target: target, + }); + } setScopedDecoratorData(context, $flattenProperty, flattenPropertyKey, target, true, scope); // eslint-disable-line deprecation/deprecation } diff --git a/packages/typespec-client-generator-core/src/lib.ts b/packages/typespec-client-generator-core/src/lib.ts index 8d39e92393..178e82742f 100644 --- a/packages/typespec-client-generator-core/src/lib.ts +++ b/packages/typespec-client-generator-core/src/lib.ts @@ -166,6 +166,12 @@ export const $lib = createTypeSpecLibrary({ nonDecorator: paramMessage`Client name: "${"name"}" is defined somewhere causing nameing conflicts in language scope: "${"scope"}"`, }, }, + "flatten-polymorphism": { + severity: "error", + messages: { + default: `Flattening polymorphic types is not supported.`, + }, + }, }, }); diff --git a/packages/typespec-client-generator-core/test/decorators.test.ts b/packages/typespec-client-generator-core/test/decorators.test.ts index b9ccd8e6be..45bf65840e 100644 --- a/packages/typespec-client-generator-core/test/decorators.test.ts +++ b/packages/typespec-client-generator-core/test/decorators.test.ts @@ -2566,6 +2566,34 @@ describe("typespec-client-generator-core: decorators", () => { code: "decorator-wrong-target", }); }); + + it("throws error when used on a polymorphism type", async () => { + const diagnostics = await runner.diagnose(` + @service({}) + @test namespace MyService { + #suppress "deprecated" "@flattenProperty decorator is not recommended to use." + @test + model Model1{ + @flattenProperty + child: Model2; + } + + @test + @discriminator("kind") + model Model2{ + kind: string; + } + + @test + @route("/func1") + op func1(@body body: Model1): void; + } + `); + + expectDiagnostics(diagnostics, { + code: "@azure-tools/typespec-client-generator-core/flatten-polymorphism", + }); + }); }); describe("@clientName", () => { From 4f944f9dac15268ba0e3356a982a85401865b3fb Mon Sep 17 00:00:00 2001 From: Mingzhe Huang Date: Wed, 24 Jul 2024 09:56:57 +0800 Subject: [PATCH 2/5] Update .chronus/changes/tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md Co-authored-by: Timothee Guerin --- .../tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chronus/changes/tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md b/.chronus/changes/tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md index c2cc2aa113..0ec2696c11 100644 --- a/.chronus/changes/tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md +++ b/.chronus/changes/tcgc-forbid-polymorphism-flatten-2024-6-23-22-12-38.md @@ -4,4 +4,4 @@ packages: - "@azure-tools/typespec-client-generator-core" --- -throw error when trying to flattening a model with polymorphsim \ No newline at end of file +Report error diagnostic when trying to flattening a model with polymorphism \ No newline at end of file From 36f30a5f2776429c8daf8ed296bf65c3cc7357b2 Mon Sep 17 00:00:00 2001 From: Mingzhe Huang Date: Wed, 24 Jul 2024 09:57:03 +0800 Subject: [PATCH 3/5] Update packages/typespec-client-generator-core/test/decorators.test.ts Co-authored-by: Timothee Guerin --- packages/typespec-client-generator-core/test/decorators.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typespec-client-generator-core/test/decorators.test.ts b/packages/typespec-client-generator-core/test/decorators.test.ts index 45bf65840e..2e390c896e 100644 --- a/packages/typespec-client-generator-core/test/decorators.test.ts +++ b/packages/typespec-client-generator-core/test/decorators.test.ts @@ -2569,7 +2569,7 @@ describe("typespec-client-generator-core: decorators", () => { it("throws error when used on a polymorphism type", async () => { const diagnostics = await runner.diagnose(` - @service({}) + @service @test namespace MyService { #suppress "deprecated" "@flattenProperty decorator is not recommended to use." @test From 08570643657bfc0740c99cc254cbfdc38e5583a0 Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Wed, 24 Jul 2024 10:13:59 +0800 Subject: [PATCH 4/5] remove usued operation --- .../typespec-client-generator-core/test/decorators.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/typespec-client-generator-core/test/decorators.test.ts b/packages/typespec-client-generator-core/test/decorators.test.ts index 2e390c896e..2eed1fbcbd 100644 --- a/packages/typespec-client-generator-core/test/decorators.test.ts +++ b/packages/typespec-client-generator-core/test/decorators.test.ts @@ -2583,10 +2583,6 @@ describe("typespec-client-generator-core: decorators", () => { model Model2{ kind: string; } - - @test - @route("/func1") - op func1(@body body: Model1): void; } `); From f45476cad5d954fb4d5eaab08ae6f5dffe004ff1 Mon Sep 17 00:00:00 2001 From: "Mingzhe Huang (from Dev Box)" Date: Wed, 24 Jul 2024 15:08:37 +0800 Subject: [PATCH 5/5] update error message according to review comments --- packages/typespec-client-generator-core/src/lib.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typespec-client-generator-core/src/lib.ts b/packages/typespec-client-generator-core/src/lib.ts index 178e82742f..0d49d586d3 100644 --- a/packages/typespec-client-generator-core/src/lib.ts +++ b/packages/typespec-client-generator-core/src/lib.ts @@ -169,7 +169,7 @@ export const $lib = createTypeSpecLibrary({ "flatten-polymorphism": { severity: "error", messages: { - default: `Flattening polymorphic types is not supported.`, + default: `Cannot flatten property of polymorphic type.`, }, }, },