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

Fix @path param mapping when spreading a record in operation parameters #3190

Merged
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
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
Loading