Skip to content

Commit

Permalink
remove useless path parameter from method
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh committed Oct 31, 2024
1 parent ba57ec5 commit 9fb60d6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/fix_route_param-2024-9-31-18-1-39.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

remove useless path parameter from method
26 changes: 20 additions & 6 deletions packages/typespec-client-generator-core/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export function getSdkHttpOperation(
const parameters = diagnostics.pipe(
getSdkHttpParameters(context, httpOperation, methodParameters, responsesWithBodies[0]),
);
filterOutUselessPathParameters(context, httpOperation, methodParameters);
return diagnostics.wrap({
__raw: httpOperation,
kind: "http",
Expand Down Expand Up @@ -408,12 +409,12 @@ function getSdkHttpResponseAndExceptions(
context: TCGCContext,
httpOperation: HttpOperation,
): [
{
responses: SdkHttpResponse[];
exceptions: SdkHttpErrorResponse[];
},
readonly Diagnostic[],
] {
{
responses: SdkHttpResponse[];
exceptions: SdkHttpErrorResponse[];
},
readonly Diagnostic[],
] {
const diagnostics = createDiagnosticCollector();
const responses: SdkHttpResponse[] = [];
const exceptions: SdkHttpErrorResponse[] = [];
Expand Down Expand Up @@ -628,6 +629,19 @@ function findMapping(
return undefined;
}

function filterOutUselessPathParameters(context: TCGCContext, httpOperation: HttpOperation, methodParameters: SdkMethodParameter[]) {
// for autoroute with constant or singleton arm resource operation,
// some path parameters will be added to route directly with the constant value,
// so we will remove the method parameter for consistent
for (let i = 0; i < methodParameters.length; i++) {
const param = methodParameters[i];
if (param.__raw && isPathParam(context.program, param.__raw) && httpOperation.parameters.parameters.filter(p => p.type === "path" && p.name === getWireName(context, param.__raw!)).length === 0) {
methodParameters.splice(i, 1);
i--;
}
}
}

function findRootSourceProperty(property: ModelProperty): ModelProperty {
while (property.sourceProperty) {
property = property.sourceProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from "../../src/interfaces.js";
import { SdkTestRunner, createSdkTestRunner } from "../test-host.js";
import { getServiceMethodOfClient, getServiceWithDefaultApiVersion } from "./utils.js";
import { AzureResourceManagerTestLibrary } from "@azure-tools/typespec-azure-resource-manager/testing";
import { OpenAPITestLibrary } from "@typespec/openapi/testing";

describe("typespec-client-generator-core: parameters", () => {
let runner: SdkTestRunner;
Expand Down Expand Up @@ -1087,4 +1089,46 @@ describe("typespec-client-generator-core: parameters", () => {
strictEqual(method.operation.bodyParam?.serializedName, "body");
});
});

describe("method parameter not used in operation", () => {
it("autoroute with constant", async () => {
await runner.compileWithBuiltInService(`
@autoRoute
op test(@path param: "test"): void;
`);
const sdkPackage = runner.context.sdkPackage;
const method = getServiceMethodOfClient(sdkPackage);
strictEqual(method.parameters.length, 0);
strictEqual(method.operation.parameters.length, 0);
strictEqual(method.operation.uriTemplate, "/test");
});

it("singleton resource", async () => {
const runnerWithArm = await createSdkTestRunner({
librariesToAdd: [AzureResourceManagerTestLibrary, AzureCoreTestLibrary, OpenAPITestLibrary],
autoUsings: ["Azure.ResourceManager", "Azure.Core"],
emitterName: "@azure-tools/typespec-java",
});
await runnerWithArm.compileWithBuiltInAzureResourceManagerService(`
@singleton("default")
model SingletonTrackedResource is TrackedResource<SingletonTrackedResourceProperties> {
...ResourceNameParameter<SingletonTrackedResource>;
}
model SingletonTrackedResourceProperties {
description?: string;
}
@armResourceOperations
interface Singleton {
createOrUpdate is ArmResourceCreateOrReplaceAsync<SingletonTrackedResource>;
}
`);

const sdkPackage = runnerWithArm.context.sdkPackage;
const method = getServiceMethodOfClient(sdkPackage);
deepStrictEqual(method.parameters.map(p => p.name), ["resourceGroupName", "resource", "contentType", "accept"]);
deepStrictEqual(method.operation.parameters.map(p => p.name), ["apiVersion", "subscriptionId", "resourceGroupName", "contentType", "accept"]);
});
});
});

0 comments on commit 9fb60d6

Please sign in to comment.