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

Dynamic routes with OpenAPI Hono not working under certain conditions #810

Open
constantins2001 opened this issue Nov 6, 2024 · 0 comments

Comments

@constantins2001
Copy link

Issue Summary

When using the following code to set up a route, the route appears in the Swagger documentation but results in a 404 error when accessed:

const app = new OpenAPIHono<WorkerHonoContext>();
app.route("/api/v1/users/", usersHono);

export const usersHono = new OpenAPIHono<WorkerHonoContext>();
registerUserWebsocketRoute(usersHono);

export const UserWebsocketRoute = createRoute({
    tags: ["User"],
    summary: "User Websocket",
    method: "post",
    path: "{userId}/websocket",
    request: {
        cookies: CookiesSchema,
        params: ParamsSchema,
        headers: HeadersSchema,
    },
    responses: {
        "1001": { description: "WebSocket has been upgraded" },
        "400": ZodErrorResponseConfig,
        "429": ZodErrorResponseConfig,
    },
});

userHono.openapi(UserWebsocketRoute, async (c) => {
    ...
});

Expected Behavior

The route should be accessible, as it is correctly displayed in the Swagger documentation.

Actual Behavior

Attempting to access this route returns a 404 error.

Solution

The route becomes accessible if the following adjustments are made:

  1. Update the subrouter registration:

    app.route("/api/v1/users", usersHono);
  2. Adjust the path format in the UserWebsocketRoute:

    path: "/{userId}/websocket",

Additional Context

It seems that the trailing slash in the route definition (/api/v1/users/) and the missing leading slash in UserWebsocketRoute's path may be causing the routing issue.

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

No branches or pull requests

1 participant