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 client initialization for subscriptionId #1476

Merged
merged 10 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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-tcgc-client-init-2024-8-5-13-31-51.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 to add client signature `subscriptionId` for ARM SDK
1 change: 1 addition & 0 deletions packages/typespec-client-generator-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
},
"devDependencies": {
"@azure-tools/typespec-azure-core": "workspace:~",
"@azure-tools/typespec-azure-resource-manager": "workspace:~",
"@types/node": "~18.11.19",
"@types/pluralize": "^0.0.33",
"@typespec/compiler": "workspace:~",
Expand Down
13 changes: 12 additions & 1 deletion packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,18 @@ function addDefaultClientParameters<
if (apiVersionParam) {
client.initialization.properties.push(apiVersionParam);
}
const subId = client.initialization.properties.find((x) => isSubscriptionId(context, x));
let subId = context.__clientToParameters
.get(client.__raw.type)
?.find((x) => isSubscriptionId(context, x));
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
if (!subId && context.arm) {
for (const operationGroup of listOperationGroups(context, client.__raw)) {
// if any sub operation groups have an subId param, the top level needs it as well
subId = context.__clientToParameters
.get(operationGroup.type)
?.find((x) => isSubscriptionId(context, x));
if (apiVersionParam) break;
}
}
msyyc marked this conversation as resolved.
Show resolved Hide resolved
if (subId) {
client.initialization.properties.push(subId);
}
Expand Down
37 changes: 37 additions & 0 deletions packages/typespec-client-generator-core/test/package.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing";
import { AzureResourceManagerTestLibrary } from "@azure-tools/typespec-azure-resource-manager/testing";
import { ApiKeyAuth, OAuth2Flow, Oauth2Auth } from "@typespec/http";
import { OpenAPITestLibrary } from "@typespec/openapi/testing";
import { deepStrictEqual, ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import {
Expand Down Expand Up @@ -2176,6 +2178,41 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(apiVersionOpParam.correspondingMethodParams[0], apiVersionClientParam);
});

it("global parameters", async () => {
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
const runnerWithArm = await createSdkTestRunner({
librariesToAdd: [AzureResourceManagerTestLibrary, AzureCoreTestLibrary, OpenAPITestLibrary],
autoUsings: ["Azure.ResourceManager", "Azure.Core"],
emitterName: "@azure-tools/typespec-java",
});
await runnerWithArm.compileWithBuiltInAzureResourceManagerService(`
model MyProperties {
@visibility("read")
@doc("Display name of the Azure Extended Zone.")
displayName: string;
}

@subscriptionResource
model MyModel is ProxyResource<MyProperties> {
@key("extendedZoneName")
@segment("extendedZones")
@path
name: string;
}

@armResourceOperations
interface MyInterface {
get is ArmResourceRead<MyModel>;
}
`);

const sdkPackage = runnerWithArm.context.sdkPackage;
const client = sdkPackage.clients[0].methods.find((x) => x.kind === "clientaccessor")
?.response as SdkClientType<SdkHttpOperation>;
for (const name of ["apiVersion", "subscriptionId", "endpoint", "credential"]) {
ok(client.initialization.properties.find((x) => x.name === name) !== undefined);
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
}
});

it("default api version for operation is", async () => {
await runner.compile(`
namespace Azure.ResourceManager {
Expand Down
32 changes: 32 additions & 0 deletions packages/typespec-client-generator-core/test/test-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface SdkTestRunner extends BasicTestRunner {
context: SdkContext<CreateSdkTestRunnerOptions, SdkHttpOperation>;
compileWithBuiltInService(code: string): Promise<Record<string, Type>>;
compileWithBuiltInAzureCoreService(code: string): Promise<Record<string, Type>>;
compileWithBuiltInAzureResourceManagerService(code: string): Promise<Record<string, Type>>;
compileWithCustomization(mainCode: string, clientCode: string): Promise<Record<string, Type>>;
compileWithVersionedService(code: string): Promise<Record<string, Type>>;
compileAndDiagnoseWithCustomization(
Expand Down Expand Up @@ -165,6 +166,37 @@ export async function createSdkTestRunner(
return result;
};

// compile with dummy arm service definition
sdkTestRunner.compileWithBuiltInAzureResourceManagerService =
async function compileWithBuiltInAzureResourceManagerService(code) {
const result = await baseCompile(
`
@armProviderNamespace("My.Service")
@server("http://localhost:3000", "endpoint")
@service({title: "My.Service"})
@versioned(Versions)
@armCommonTypesVersion(CommonTypes.Versions.v5)
namespace My.Service;

/** Api versions */
enum Versions {
/** 2024-04-01-preview api version */
@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)
V2024_04_01_PREVIEW: "2024-04-01-preview",
}
${code}`,
{
noEmit: true,
}
);
sdkTestRunner.context = await createSdkContextTestHelper(
sdkTestRunner.program,
options,
sdkContextOption
);
return result;
};

const mainAutoCode = [
...host.libraries
.filter((x) => x !== StandardTestLibrary)
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading