Skip to content

Commit

Permalink
[tcgc] fix @clientName with @operationGroup (#1203)
Browse files Browse the repository at this point in the history
fixes #1164

---------

Co-authored-by: iscai-msft <[email protected]>
  • Loading branch information
iscai-msft and iscai-msft authored Jul 19, 2024
1 parent ff685cb commit bbbfe20
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/namespace_client_name-2024-6-18-19-15-52.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

have @clientName work for operation groups as well
10 changes: 8 additions & 2 deletions packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { resolveVersions } from "@typespec/versioning";
import { camelCase } from "change-case";
import {
getAccess,
getClientNameOverride,
listClients,
listOperationGroups,
listOperationsInOperationGroup,
Expand Down Expand Up @@ -539,13 +540,18 @@ function createSdkClientType<
): [SdkClientType<TServiceOperation>, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
const isClient = client.kind === "SdkClient";
const clientName = isClient ? client.name : client.type.name;
let name = "";
if (isClient) {
name = client.name;
} else {
name = getClientNameOverride(context, client.type) ?? client.type.name;
}
// NOTE: getSdkMethods recursively calls createSdkClientType
const methods = diagnostics.pipe(getSdkMethods(context, client));
const docWrapper = getDocHelper(context, client.type);
const sdkClientType: SdkClientType<TServiceOperation> = {
kind: "client",
name: clientName,
name,
description: docWrapper.description,
details: docWrapper.details,
methods: methods,
Expand Down
21 changes: 21 additions & 0 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,27 @@ describe("typespec-client-generator-core: decorators", () => {
},
]);
});

it("with @clientName", async () => {
await runner.compileWithBuiltInService(
`
@operationGroup
@clientName("ClientModel")
interface Model {
op foo(): void;
}
`
);
const sdkPackage = runner.context.sdkPackage;
strictEqual(sdkPackage.clients.length, 1);
const mainClient = sdkPackage.clients[0];
strictEqual(mainClient.methods.length, 1);

const clientAccessor = mainClient.methods[0];
strictEqual(clientAccessor.kind, "clientaccessor");
strictEqual(clientAccessor.response.kind, "client");
strictEqual(clientAccessor.response.name, "ClientModel");
});
});

describe("listOperationGroups without @client and @operationGroup", () => {
Expand Down

0 comments on commit bbbfe20

Please sign in to comment.