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 onClient setting for parameters applied to interface @clientInitialization #1554

Merged
merged 2 commits into from
Sep 18, 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/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
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
Loading