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

[http-server-javascript] Fix an annoying bug with router path matching. #4115

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 @@
---
changeKind: fix
packages:
- typespec-vs
- "@typespec/http-server-javascript"
---

Fixed a router bug where paths would sometimes fail to match after a parameter was bound.
7 changes: 5 additions & 2 deletions packages/http-server-javascript/src/http/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,11 @@ function* emitRouteHandler(

yield `else {`;
const paramName = parameters.length === 1 ? parameters[0] : "param";
yield ` const [${paramName}, rest] = path.split("/", 1);`;
yield ` path = rest ?? "";`;
const idxName = `__${parseCase(paramName).snakeCase}_idx`;
yield ` let ${idxName} = path.indexOf("/");`;
yield ` ${idxName} = ${idxName} === -1 ? path.length : ${idxName};`;
yield ` const ${paramName} = path.slice(0, ${idxName});`;
yield ` path = path.slice(${idxName});`;
if (parameters.length !== 1) {
for (const p of parameters) {
yield ` const ${parseCase(p).camelCase} = param;`;
Expand Down
Loading