Skip to content

Commit

Permalink
[tcgc] fix @clientName lost after adding versioning support (#731)
Browse files Browse the repository at this point in the history
fixes #730

---------

Co-authored-by: iscai-msft <[email protected]>
tadelesh and iscai-msft authored Apr 24, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 40318bc commit 1a4fd0d
Showing 3 changed files with 163 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .chronus/changes/fix_clientname-2024-3-24-17-43-5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

fix `@clientName` lost after adding versioning support
2 changes: 1 addition & 1 deletion packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
@@ -946,7 +946,7 @@ export function $clientName(
context.program.checker.resolveTypeReference(
(context.decoratorTarget as AugmentDecoratorStatementNode).targetType
)
) !== entity
)?.node !== entity.node
) {
return;
}
155 changes: 155 additions & 0 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {

0 comments on commit 1a4fd0d

Please sign in to comment.