Skip to content

Commit

Permalink
Fix @path param mapping when spreading a record in operation parame…
Browse files Browse the repository at this point in the history
…ters (#3190)

fix #3051
  • Loading branch information
timotheeguerin committed Apr 23, 2024
1 parent 5fa3d78 commit b7e45f0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/http"
---

Fix `@path` param mapping when spreading a record in operation parameters
2 changes: 1 addition & 1 deletion packages/http/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const $lib = createTypeSpecLibrary({
"missing-path-param": {
severity: "error",
messages: {
default: paramMessage`Path contains parameter ${"param"} but wasn't found in given parameters`,
default: paramMessage`Route reference parameter '${"param"}' but wasn't found in operation parameters`,
},
},
"optional-path-param": {
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export function gatherMetadata(
rootMapOut?: Map<ModelProperty, ModelProperty>
): Set<ModelProperty> {
const metadata = new Map<string, ModelProperty>();
if (type.kind !== "Model" || type.indexer || type.properties.size === 0) {
if (type.kind !== "Model" || type.properties.size === 0) {
return new Set();
}

Expand Down
27 changes: 27 additions & 0 deletions packages/http/test/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { HttpOperation, getRoutePath } from "../src/index.js";
import {
compileOperations,
createHttpTestRunner,
diagnoseOperations,
getOperations,
getRoutesFor,
} from "./test-host.js";
Expand Down Expand Up @@ -150,6 +151,32 @@ describe("http: routes", () => {
});
});

describe("@route path parameters mapping", () => {
it("maps route interpolated params to the operation param", async () => {
const routes = await getRoutesFor(
`@route("/foo/{myParam}/") op test(@path myParam: string): void;`
);
deepStrictEqual(routes, [{ verb: "get", path: "/foo/{myParam}", params: ["myParam"] }]);
});

it("maps route interpolated params to the operation param when operation spread items", async () => {
const routes = await getRoutesFor(
`@route("/foo/{myParam}/") op test(@path myParam: string, ...Record<string>): void;`
);
deepStrictEqual(routes, [{ verb: "post", path: "/foo/{myParam}", params: ["myParam"] }]);
});

it("emit diagnostic if interpolated param is missing in param list", async () => {
const diagnostics = await diagnoseOperations(
`@route("/foo/{myParam}/") op test(@path other: string): void;`
);
expectDiagnostics(diagnostics, {
code: "@typespec/http/missing-path-param",
message: "Route reference parameter 'myParam' but wasn't found in operation parameters",
});
});
});

describe("path parameters when no explicit @route", () => {
it("uses the name of the parameter by default and wraps in {}", async () => {
const routes = await getRoutesFor(`op test(@path myParam: string): void;`);
Expand Down
8 changes: 8 additions & 0 deletions packages/http/test/test-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export async function compileOperations(
return [details, diagnostics];
}

export async function diagnoseOperations(
code: string,
routeOptions?: RouteResolutionOptions
): Promise<readonly Diagnostic[]> {
const [_, diagnostics] = await compileOperations(code, routeOptions);
return diagnostics;
}

export async function getOperationsWithServiceNamespace(
code: string,
routeOptions?: RouteResolutionOptions
Expand Down

0 comments on commit b7e45f0

Please sign in to comment.