diff --git a/.chronus/changes/fix-tcgc-client-init-2024-8-5-13-31-51.md b/.chronus/changes/fix-tcgc-client-init-2024-8-5-13-31-51.md new file mode 100644 index 0000000000..a2bbccd0e2 --- /dev/null +++ b/.chronus/changes/fix-tcgc-client-init-2024-8-5-13-31-51.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +Fix to add client signature `subscriptionId` for ARM SDK diff --git a/packages/typespec-client-generator-core/package.json b/packages/typespec-client-generator-core/package.json index b713d0118f..7cd67605a3 100644 --- a/packages/typespec-client-generator-core/package.json +++ b/packages/typespec-client-generator-core/package.json @@ -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:~", diff --git a/packages/typespec-client-generator-core/src/package.ts b/packages/typespec-client-generator-core/src/package.ts index e6038d0c82..978b0f27f0 100644 --- a/packages/typespec-client-generator-core/src/package.ts +++ b/packages/typespec-client-generator-core/src/package.ts @@ -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)); + 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; + } + } if (subId) { client.initialization.properties.push(subId); } diff --git a/packages/typespec-client-generator-core/test/package.test.ts b/packages/typespec-client-generator-core/test/package.test.ts index 34c7ed6609..6a40339599 100644 --- a/packages/typespec-client-generator-core/test/package.test.ts +++ b/packages/typespec-client-generator-core/test/package.test.ts @@ -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 { @@ -2176,6 +2178,43 @@ describe("typespec-client-generator-core: package", () => { strictEqual(apiVersionOpParam.correspondingMethodParams[0], apiVersionClientParam); }); + it("client level signatures by default", async () => { + 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 { + @key("extendedZoneName") + @segment("extendedZones") + @path + name: string; + } + + @armResourceOperations + interface MyInterface { + get is ArmResourceRead; + } + `); + + const sdkPackage = runnerWithArm.context.sdkPackage; + const client = sdkPackage.clients[0].methods.find((x) => x.kind === "clientaccessor") + ?.response as SdkClientType; + for (const name of ["apiVersion", "subscriptionId", "endpoint", "credential"]) { + const item = client.initialization.properties.find((x) => x.name === name); + ok(item !== undefined); + ok(item.onClient); + } + }); + it("default api version for operation is", async () => { await runner.compile(` namespace Azure.ResourceManager { diff --git a/packages/typespec-client-generator-core/test/test-host.ts b/packages/typespec-client-generator-core/test/test-host.ts index e8980de81b..87a72b016f 100644 --- a/packages/typespec-client-generator-core/test/test-host.ts +++ b/packages/typespec-client-generator-core/test/test-host.ts @@ -34,6 +34,7 @@ export interface SdkTestRunner extends BasicTestRunner { context: SdkContext; compileWithBuiltInService(code: string): Promise>; compileWithBuiltInAzureCoreService(code: string): Promise>; + compileWithBuiltInAzureResourceManagerService(code: string): Promise>; compileWithCustomization(mainCode: string, clientCode: string): Promise>; compileWithVersionedService(code: string): Promise>; compileAndDiagnoseWithCustomization( @@ -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) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 62402179ab..fde690ae41 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2219,6 +2219,9 @@ importers: '@azure-tools/typespec-azure-core': specifier: workspace:~ version: link:../typespec-azure-core + '@azure-tools/typespec-azure-resource-manager': + specifier: workspace:~ + version: link:../typespec-azure-resource-manager '@types/node': specifier: ~18.11.19 version: 18.11.19