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

Keep trailing slash when building http routes #3737

Merged
merged 4 commits into from
Jul 15, 2024
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
15 changes: 15 additions & 0 deletions .chronus/changes/http-trailing-slash-2024-6-2-23-8-57.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: breaking
packages:
- "@typespec/http"
---

Keep trailing slash when building http routes, this is breaking if you used to have `@route()` ending with `/`.

| TypeSpec | Before | After |
| ---------------------------------------------------------------- | ----------------- | ------------------ |
| `@route("users/")` | `users` | `users/` |
| `@route("users")` | `users` | `users` |
| on interface `@route("users/")` and on op `@route("addresses/")` | `users/addresses` | `users/addresses/` |
| on interface `@route("users/")` and on op `@route("addresses")` | `users/addresses` | `users/addresses` |
26 changes: 15 additions & 11 deletions packages/http/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,32 @@ import { extractParamsFromPath } from "./utils.js";
// The set of allowed segment separator characters
const AllowedSegmentSeparators = ["/", ":"];

function normalizeFragment(fragment: string) {
function normalizeFragment(fragment: string, trimLast = false) {
if (fragment.length > 0 && AllowedSegmentSeparators.indexOf(fragment[0]) < 0) {
// Insert the default separator
fragment = `/${fragment}`;
}

// Trim any trailing slash
return fragment.replace(/\/$/g, "");
if (trimLast && fragment[fragment.length - 1] === "/") {
return fragment.slice(0, -1);
}
return fragment;
}

function joinPathSegments(rest: string[]) {
let current = "";
for (const [index, segment] of rest.entries()) {
current += normalizeFragment(segment, index < rest.length - 1);
}
return current;
}

function buildPath(pathFragments: string[]) {
// Join all fragments with leading and trailing slashes trimmed
const path =
pathFragments.length === 0
? "/"
: pathFragments
.map(normalizeFragment)
.filter((x) => x !== "")
.join("");
const path = pathFragments.length === 0 ? "/" : joinPathSegments(pathFragments);

// The final path must start with a '/'
return path.length > 0 && path[0] === "/" ? path : `/${path}`;
return path[0] === "/" ? path : `/${path}`;
}

export function resolvePathAndParameters(
Expand Down
27 changes: 25 additions & 2 deletions packages/http/test/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ 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;`
`@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;`
`@route("/foo/{myParam}") op test(@path myParam: string, ...Record<string>): void;`
);
deepStrictEqual(routes, [{ verb: "post", path: "/foo/{myParam}", params: ["myParam"] }]);
});
Expand Down Expand Up @@ -280,6 +280,29 @@ describe("http: routes", () => {
deepStrictEqual(routes, [{ verb: "get", path: "/", params: [] }]);
});

it("keeps trailing / at the end of the route", async () => {
const routes = await getRoutesFor(
`
@route("/foo/") op index(): void;
`
);

deepStrictEqual(routes, [{ verb: "get", path: "/foo/", params: [] }]);
});

it("merge trailing and leading / when combining container path", async () => {
const routes = await getRoutesFor(
`
@route("/foo/")
interface Foo {
@route("/bar/") op index(): void;
}
`
);

deepStrictEqual(routes, [{ verb: "get", path: "/foo/bar/", params: [] }]);
});

it("join / route segments correctly", async () => {
const routes = await getRoutesFor(
`
Expand Down
Loading