Skip to content

Commit

Permalink
[tcgc] fix onClient setting for parameters applied to interface @cl…
Browse files Browse the repository at this point in the history
…ientInitialization (#1554)

fixes #1532

---------

Co-authored-by: iscai-msft <[email protected]>
  • Loading branch information
iscai-msft and iscai-msft committed Sep 18, 2024
1 parent f1af9d9 commit d8c73dd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/onClientSetting-2024-8-17-17-22-46.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 `onClient` setting for client initialization parameters applied to an interface
8 changes: 6 additions & 2 deletions packages/typespec-client-generator-core/src/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,12 @@ export function getHttpBodySpreadModel(context: TCGCContext, type: Model): Model
return type;
}

export function isOnClient(context: TCGCContext, type: ModelProperty): boolean {
const namespace = type.model?.namespace;
export function isOnClient(
context: TCGCContext,
type: ModelProperty,
operation?: Operation
): boolean {
const namespace = operation ? getLocationOfOperation(operation) : type.model?.namespace;
return (
isSubscriptionId(context, type) ||
isApiVersion(context, type) ||
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ export function getSdkModelPropertyTypeBase(
}
const docWrapper = getDocHelper(context, type);
const name = getPropertyNames(context, type)[0];
const onClient = isOnClient(context, type);
const onClient = isOnClient(context, type, operation);
return diagnostics.wrap({
__raw: type,
description: docWrapper.description,
Expand Down
51 changes: 51 additions & 0 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3873,6 +3873,53 @@ describe("typespec-client-generator-core: decorators", () => {
strictEqual(blobNameOpParam.name, "blobName");
strictEqual(blobNameOpParam.correspondingMethodParams.length, 1);
strictEqual(blobNameOpParam.correspondingMethodParams[0], blobName);
strictEqual(blobNameOpParam.onClient, true);
});

it("On Interface", async () => {
await runner.compileWithBuiltInService(
`
model clientInitModel
{
p1: string;
}
@route("/bump")
@clientInitialization(clientInitModel)
interface bumpParameter {
@route("/op1")
@doc("bump parameter")
@post
@convenientAPI(true)
op op1(@path p1: string, @query q1: string): void;
@route("/op2")
@doc("bump parameter")
@post
@convenientAPI(true)
op op2(@path p1: string): void;
}
`
);
const sdkPackage = runner.context.sdkPackage;
const clientAccessor = sdkPackage.clients[0].methods[0];
strictEqual(clientAccessor.kind, "clientaccessor");
const bumpParameterClient = clientAccessor.response;

const methods = bumpParameterClient.methods;
strictEqual(methods.length, 2);

const op1Method = methods.find((x) => x.name === "op1");
ok(op1Method);
strictEqual(op1Method.kind, "basic");
strictEqual(op1Method.parameters.length, 1);
strictEqual(op1Method.parameters[0].name, "q1");
const op1Op = op1Method.operation;
strictEqual(op1Op.parameters.length, 2);
strictEqual(op1Op.parameters[0].name, "p1");
strictEqual(op1Op.parameters[0].onClient, true);
strictEqual(op1Op.parameters[1].name, "q1");
strictEqual(op1Op.parameters[1].onClient, false);
});
it("subclient", async () => {
await runner.compileWithCustomization(
Expand Down Expand Up @@ -3956,6 +4003,7 @@ describe("typespec-client-generator-core: decorators", () => {
strictEqual(blobNameOpParam.name, "blobName");
strictEqual(blobNameOpParam.correspondingMethodParams.length, 1);
strictEqual(blobNameOpParam.correspondingMethodParams[0], blobClientBlobInitializationProp);
strictEqual(blobNameOpParam.onClient, true);
});
it("some methods don't have client initialization params", async () => {
await runner.compileWithCustomization(
Expand Down Expand Up @@ -4004,6 +4052,7 @@ describe("typespec-client-generator-core: decorators", () => {
strictEqual(blobNameOpParam.name, "blobName");
strictEqual(blobNameOpParam.correspondingMethodParams.length, 1);
strictEqual(blobNameOpParam.correspondingMethodParams[0], blobName);
strictEqual(blobNameOpParam.onClient, true);

const noClientParamsMethod = methods[1];
strictEqual(noClientParamsMethod.name, "noClientParams");
Expand Down Expand Up @@ -4131,6 +4180,8 @@ describe("typespec-client-generator-core: decorators", () => {
strictEqual(op.parameters.length, 2);
strictEqual(op.parameters[0].correspondingMethodParams[0], blobName);
strictEqual(op.parameters[1].correspondingMethodParams[0], containerName);
strictEqual(op.parameters[0].onClient, true);
strictEqual(op.parameters[1].onClient, true);
});

it("redefine client structure", async () => {
Expand Down

0 comments on commit d8c73dd

Please sign in to comment.