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

mergePath incorrectly adds a trailing slash when first path segment is / #3909

Closed
luxass opened this issue Feb 9, 2025 · 0 comments · Fixed by #3910
Closed

mergePath incorrectly adds a trailing slash when first path segment is / #3909

luxass opened this issue Feb 9, 2025 · 0 comments · Fixed by #3910
Labels

Comments

@luxass
Copy link
Contributor

luxass commented Feb 9, 2025

What version of Hono are you using?

4.7.0

What runtime/platform is your app running on? (with version if possible)

Cloudflare Workers

What steps can reproduce the bug?

This issue mainly happens when you combine Hono with Hono's Zod Openapi, but since the issue is coming from Hono itself i have created the issue here.

By using this code here:

const app = new OpenAPIHono<HonoContext>();

const v1 = new OpenAPIHono<HonoContext>().basePath("/v1");

v1.openapi(
  createRoute({
    method: "get",
    path: "/",
    responses: {
      200: {
        description: "Get message",
      },
    },
  }),
  (c) => c.json({ message: "Hello" }),
);

app.route("/", v1);

app.doc("/doc", {
  openapi: "3.0.0",
  info: {
    version: "1.0.0",
    title: "My API",
  },
});

You can request /v1 which will respond correctly, but in the final openapi document the route is generated as the following: /v1/ which results in scalar, swagger and other such tools to use the wrong path for requesting the endpoint.

On the Zod Openapi middleware it will provide the following paths to mergePath: mergePath("/", "/v1", "/") which results in this /v1/.

It seems the issue comes from these lines, since after the first iteration the p variable is now /, and then this condition is true.

hono/src/utils/url.ts

Lines 145 to 148 in 6ceb1ab

if (p.at(-1) === '/') {
p = p.slice(0, -1)
endsWithSlash = true
}

What is the expected behavior?

The openapi document should not have /v1/ but instead /v1.

What do you see instead?

That the openapi document has /v1/ and not /v1

Additional information

It seems to only happen when there is a route with only / as the path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant