From 1a4fd0d2378be1e44ba08ec7827d57675f1cd94d Mon Sep 17 00:00:00 2001 From: Chenjie Shi Date: Thu, 25 Apr 2024 02:17:59 +0800 Subject: [PATCH] [tcgc] fix @clientName lost after adding versioning support (#731) fixes https://github.com/Azure/typespec-azure/issues/730 --------- Co-authored-by: iscai-msft --- .../fix_clientname-2024-3-24-17-43-5.md | 7 + .../src/decorators.ts | 2 +- .../test/decorators.test.ts | 155 ++++++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 .chronus/changes/fix_clientname-2024-3-24-17-43-5.md diff --git a/.chronus/changes/fix_clientname-2024-3-24-17-43-5.md b/.chronus/changes/fix_clientname-2024-3-24-17-43-5.md new file mode 100644 index 0000000000..5a28034613 --- /dev/null +++ b/.chronus/changes/fix_clientname-2024-3-24-17-43-5.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +fix `@clientName` lost after adding versioning support \ 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 8e80ded908..89cdf9e2b7 100644 --- a/packages/typespec-client-generator-core/src/decorators.ts +++ b/packages/typespec-client-generator-core/src/decorators.ts @@ -946,7 +946,7 @@ export function $clientName( context.program.checker.resolveTypeReference( (context.decoratorTarget as AugmentDecoratorStatementNode).targetType ) - ) !== entity + )?.node !== entity.node ) { return; } diff --git a/packages/typespec-client-generator-core/test/decorators.test.ts b/packages/typespec-client-generator-core/test/decorators.test.ts index 2542af2efe..b619c1d71e 100644 --- a/packages/typespec-client-generator-core/test/decorators.test.ts +++ b/packages/typespec-client-generator-core/test/decorators.test.ts @@ -2498,6 +2498,94 @@ describe("typespec-client-generator-core: decorators", () => { strictEqual(getClientNameOverride(runner.context, func1), "func1Rename"); strictEqual(getClientNameOverride(runner.context, func2), undefined); }); + + it("@clientName with scope of versioning", async () => { + const testCode = ` + @service({ + title: "Contoso Widget Manager", + }) + @versioned(Contoso.WidgetManager.Versions) + namespace Contoso.WidgetManager; + + enum Versions { + v1, + v2, + } + + @clientName("TestJava", "java") + @clientName("TestCSharp", "csharp") + model Test {} + op test(@body body: Test): void; + `; + + // java + { + const runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-java" }); + await runner.compile(testCode); + strictEqual(runner.context.experimental_sdkPackage.models[0].name, "TestJava"); + } + + // csharp + { + const runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-csharp" }); + await runner.compile(testCode); + strictEqual(runner.context.experimental_sdkPackage.models[0].name, "TestCSharp"); + } + + // python + { + const runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-python" }); + await runner.compile(testCode); + strictEqual(runner.context.experimental_sdkPackage.models[0].name, "Test"); + } + }); + + it("augmented @clientName with scope of versioning", async () => { + const testCode = ` + @service({ + title: "Contoso Widget Manager", + }) + @versioned(Contoso.WidgetManager.Versions) + namespace Contoso.WidgetManager; + + enum Versions { + v1, + v2, + } + + + model Test {} + op test(@body body: Test): void; + `; + + const customization = ` + namespace Customizations; + + @@clientName(Contoso.WidgetManager.Test, "TestCSharp", "csharp"); + @@clientName(Contoso.WidgetManager.Test, "TestJava", "java"); + `; + + // java + { + const runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-java" }); + await runner.compileWithCustomization(testCode, customization); + strictEqual(runner.context.experimental_sdkPackage.models[0].name, "TestJava"); + } + + // csharp + { + const runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-csharp" }); + await runner.compileWithCustomization(testCode, customization); + strictEqual(runner.context.experimental_sdkPackage.models[0].name, "TestCSharp"); + } + + // python + { + const runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-python" }); + await runner.compileWithCustomization(testCode, customization); + strictEqual(runner.context.experimental_sdkPackage.models[0].name, "Test"); + } + }); }); describe("versioning projection", () => { @@ -3043,6 +3131,73 @@ describe("typespec-client-generator-core: decorators", () => { ["v1", "v2", "v3"] ); }); + + it("model only used in new version", async () => { + const tsp = ` + @service({ + title: "Contoso Widget Manager", + }) + @versioned(Contoso.WidgetManager.Versions) + namespace Contoso.WidgetManager; + + enum Versions { + v2023_11_01_preview: "2023-11-01-preview", + v2023_11_01: "2023-11-01", + } + + model PreviewModel { + betaFeature: string; + } + + model StableModel { + stableFeature: string; + } + + @added(Versions.v2023_11_01_preview) + @removed(Versions.v2023_11_01) + @route("/preview") + op previewFunctionality(...PreviewModel): void; + + @route("/stable") + op stableFunctionality(...StableModel): void; + `; + + let runnerWithVersion = await createSdkTestRunner({ + "api-version": "2023-11-01-preview", + emitterName: "@azure-tools/typespec-python", + }); + + await runnerWithVersion.compile(tsp); + + strictEqual(runnerWithVersion.context.experimental_sdkPackage.clients.length, 1); + strictEqual(runnerWithVersion.context.experimental_sdkPackage.clients[0].methods.length, 2); + strictEqual( + runnerWithVersion.context.experimental_sdkPackage.clients[0].methods[0].name, + "previewFunctionality" + ); + strictEqual( + runnerWithVersion.context.experimental_sdkPackage.clients[0].methods[1].name, + "stableFunctionality" + ); + strictEqual(runnerWithVersion.context.experimental_sdkPackage.models.length, 2); + strictEqual(runnerWithVersion.context.experimental_sdkPackage.models[0].name, "PreviewModel"); + strictEqual(runnerWithVersion.context.experimental_sdkPackage.models[1].name, "StableModel"); + + runnerWithVersion = await createSdkTestRunner({ + emitterName: "@azure-tools/typespec-python", + }); + + await runnerWithVersion.compile(tsp); + + strictEqual(runnerWithVersion.context.experimental_sdkPackage.clients.length, 1); + strictEqual(runnerWithVersion.context.experimental_sdkPackage.clients[0].methods.length, 1); + strictEqual( + runnerWithVersion.context.experimental_sdkPackage.clients[0].methods[0].name, + "stableFunctionality" + ); + strictEqual(runnerWithVersion.context.experimental_sdkPackage.models.length, 1); + strictEqual(runnerWithVersion.context.experimental_sdkPackage.models[0].name, "StableModel"); + }); }); describe("versioning impact for apis", () => {