Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tcgc] fix @clientName lost after adding versioning support #731

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ export function $clientName(
context.program.checker.resolveTypeReference(
(context.decoratorTarget as AugmentDecoratorStatementNode).targetType
)
) !== entity
)?.node !== entity.node
) {
return;
}
Expand Down
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
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down
Loading