Skip to content

Commit

Permalink
[tcgc] use root source property to map operation params to method (#1761
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tadelesh authored Oct 30, 2024
1 parent 5e2ee2a commit ba57ec5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/fix_param_mapping-2024-9-29-13-22-11.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

use root source property to map operation params to method
9 changes: 8 additions & 1 deletion packages/typespec-client-generator-core/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ function findMapping(
if (
methodParam.__raw &&
serviceParam.__raw &&
methodParam.__raw.node === serviceParam.__raw.node
findRootSourceProperty(methodParam.__raw) === findRootSourceProperty(serviceParam.__raw)
) {
return methodParam;
}
Expand Down Expand Up @@ -628,6 +628,13 @@ function findMapping(
return undefined;
}

function findRootSourceProperty(property: ModelProperty): ModelProperty {
while (property.sourceProperty) {
property = property.sourceProperty;
}
return property;
}

function getCollectionFormat(
context: TCGCContext,
type: ModelProperty,
Expand Down
64 changes: 62 additions & 2 deletions packages/typespec-client-generator-core/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,9 @@ describe("typespec-client-generator-core: package", () => {
@parentResource(Widget)
model WidgetAnalytics {
@key("analyticsId")
@doc("The identifier for the analytics object. There is only one named 'current'.")
@doc("The identifier for the analytics object.")
@visibility("read")
id: "current";
id: string;
@doc("The number of uses of the widget.")
useCount: int64;
Expand Down Expand Up @@ -1175,6 +1175,66 @@ describe("typespec-client-generator-core: package", () => {
ok(clientRequestIdProperty);
strictEqual(clientRequestIdProperty.kind, "header");
});

it("azure widget getWidgetAnalytics", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
autoUsings: ["Azure.Core", "Azure.Core.Traits"],
emitterName: "@azure-tools/typespec-java",
});
await compileAzureWidgetService(
runnerWithCore,
`
@doc("Get a WidgetAnalytics")
getWidgetAnalytics is Operations.ResourceRead<WidgetAnalytics>;
`,
);
const sdkPackage = runnerWithCore.context.sdkPackage;
const parentClient = sdkPackage.clients.filter(
(c) => c.initialization.access === "public",
)[0];
const method = getServiceMethodOfClient(sdkPackage);
strictEqual(parentClient.name, "WidgetManagerClient");
strictEqual(method.name, "getWidgetAnalytics");
strictEqual(method.kind, "basic");
strictEqual(method.parameters.length, 8);

const methodWidgetName = method.parameters.find((p) => p.name === "widgetName");
ok(methodWidgetName);
strictEqual(methodWidgetName.kind, "method");
strictEqual(methodWidgetName.isApiVersionParam, false);
deepStrictEqual(methodWidgetName.apiVersions, ["2022-08-30"]);
strictEqual(methodWidgetName.onClient, false);
strictEqual(methodWidgetName.optional, false);

const operationWidgetName = method.operation.parameters.find((x) => x.name === "widgetName");
ok(operationWidgetName);
strictEqual(operationWidgetName.kind, "path");
strictEqual(operationWidgetName.name, "widgetName");
strictEqual(operationWidgetName.serializedName, "widgetName");
strictEqual(operationWidgetName.onClient, false);
strictEqual(operationWidgetName.correspondingMethodParams.length, 1);
strictEqual(operationWidgetName.correspondingMethodParams[0], methodWidgetName);

const methodAnalyticsId = method.parameters.find((p) => p.name === "analyticsId");
ok(methodAnalyticsId);
strictEqual(methodAnalyticsId.kind, "method");
strictEqual(methodAnalyticsId.isApiVersionParam, false);
deepStrictEqual(methodAnalyticsId.apiVersions, ["2022-08-30"]);
strictEqual(methodAnalyticsId.onClient, false);
strictEqual(methodAnalyticsId.optional, false);

const operationAnalyticsId = method.operation.parameters.find(
(x) => x.name === "analyticsId",
);
ok(operationAnalyticsId);
strictEqual(operationAnalyticsId.kind, "path");
strictEqual(operationAnalyticsId.name, "analyticsId");
strictEqual(operationAnalyticsId.serializedName, "analyticsId");
strictEqual(operationAnalyticsId.onClient, false);
strictEqual(operationAnalyticsId.correspondingMethodParams.length, 1);
strictEqual(operationAnalyticsId.correspondingMethodParams[0], methodAnalyticsId);
});
});

describe("versioning", () => {
Expand Down

0 comments on commit ba57ec5

Please sign in to comment.