diff --git a/.chronus/changes/copilot-fix-regression-http-1-10-1-2026-2-18-18-33-49.md b/.chronus/changes/copilot-fix-regression-http-1-10-1-2026-2-18-18-33-49.md new file mode 100644 index 00000000000..0d220f1033f --- /dev/null +++ b/.chronus/changes/copilot-fix-regression-http-1-10-1-2026-2-18-18-33-49.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http" +--- + +Fix route joining to preserve trailing `/` when it would not result in `//` \ No newline at end of file diff --git a/packages/http/src/route.ts b/packages/http/src/route.ts index d60cdd823df..1e56f249c15 100644 --- a/packages/http/src/route.ts +++ b/packages/http/src/route.ts @@ -37,27 +37,28 @@ function needsSlashPrefix(fragment: string) { ); } -function normalizeFragment(fragment: string, trimLast = false) { +function normalizeFragment(fragment: string) { if (needsSlashPrefix(fragment)) { // Insert the default separator fragment = `/${fragment}`; } - if (trimLast && fragment[fragment.length - 1] === "/") { - return fragment.slice(0, -1); - } return fragment; } /** * Joins route fragments, adding a slash when a fragment lacks a recognized separator - * and trimming one trailing slash from non-final fragments. Preserves URI-template path expansions. + * and merging overlapping slashes at fragment boundaries. Preserves URI-template path expansions. * An empty input returns an empty string. */ export function joinPathSegments(rest: string[]) { let current = ""; - for (const [index, segment] of rest.entries()) { - current += normalizeFragment(segment, index < rest.length - 1); + for (const segment of rest) { + const normalized = normalizeFragment(segment); + if (current.endsWith("/") && (normalized.startsWith("/") || normalized.startsWith("{/"))) { + current = current.slice(0, -1); + } + current += normalized; } return current; } diff --git a/packages/http/test/routes.test.ts b/packages/http/test/routes.test.ts index 280d1b6253c..d50b197c5b1 100644 --- a/packages/http/test/routes.test.ts +++ b/packages/http/test/routes.test.ts @@ -342,6 +342,25 @@ describe("http: routes", () => { deepStrictEqual(routes, [{ verb: "get", path: `/abc${sep}restype=container`, params: [] }]); }); + + it("keeps trailing / on parent route when joining with child route", async () => { + const routes = await getRoutesFor( + ` + @route("{blobName}/") + interface Container { + @put @route("${sep}some-query=true") op foo(blobName: string): void; + } + `, + ); + + deepStrictEqual(routes, [ + { + verb: "put", + path: `/{blobName}/${sep}some-query=true`, + params: ["blobName"], + }, + ]); + }); }); describe("joinPathSegments", () => { @@ -621,6 +640,14 @@ describe("uri template", () => { const op = await getOp(`@route("/foo") op foo(${param}): void;`); expect(op.uriTemplate).toEqual(expectedUri); }); + + it.each([`@path(#{style: "path"}) one: string`, "@path one?: string"])( + "merges trailing / with path expansion for %s", + async (param) => { + const op = await getOp(`@route("/foo/") op foo(${param}): void;`); + expect(op.uriTemplate).toEqual("/foo{/one}"); + }, + ); }); describe("warn when path interpolation would result in duplicate //", () => {