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

Swagger Plugin Not Working With Elysia Prefix #1071

Closed
wolfenxx opened this issue Feb 19, 2025 · 3 comments
Closed

Swagger Plugin Not Working With Elysia Prefix #1071

wolfenxx opened this issue Feb 19, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@wolfenxx
Copy link

What version of Elysia is running?

1.2.15 and elysia/swagger version 1.2.1

What platform is your computer?

NixOS

What steps can reproduce the bug?

Instantiate a new Elysia instance with a prefix, add swagger plugin and some routes. Then navigate to the swagger path.

Here's a one-pager. Copy this into index.ts and run

import { Elysia } from "elysia";
import { swagger } from "@elysiajs/swagger";

const authController = new Elysia({
  prefix: "/auth",
}).post("/signup", () => 1);

new Elysia({ prefix: "/app/api/v1" })
  .use(swagger({ path: "/app/api/v1/swagger" }))
  .use(authController)
  .listen(3000);

Then navigate to localhost:3000/app/api/v1/swagger. You can see in console it throws a 404 not found for this swagger path.

I've also tried leaving swagger plugin empty (with defaults) so .use(swagger()) but this results in an empty swagger page that is trying to fetch the data from /swagger/json

What is the expected behavior?

Swagger page should display all routes

What do you see instead?

404 not found error

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

yes

@wolfenxx wolfenxx added the bug Something isn't working label Feb 19, 2025
@goldenstein64
Copy link

goldenstein64 commented Feb 21, 2025

Both the app and swagger plugin have a prefix, so both of them get joined together in one route. In your example, the swagger API can be accessed at localhost:3000/app/api/v1/app/api/v1/swagger.

Tangent: even at that endpoint, the documentation is still empty, and I think it's because the client (HTML template) is looking for the wrong JSON endpoint. The client tries to GET /swagger-prefix/json instead of the endpoint the app is actually serving, /app-prefix/swagger-prefix/json. The workaround for that is to attach swagger() to an app with no prefix. i.e.

import { Elysia } from "elysia";
import { swagger } from "@elysiajs/swagger";

const authController = new Elysia({
  prefix: "/auth",
}).post("/signup", () => 1);

const v1 = new Elysia({ prefix: "/app/api/v1" }).use(authController);

new Elysia()
  .use(swagger({ path: "/app/api/v1/swagger" }))
  .use(v1)
  .listen(3000);

Fixing that would require resolving the full path instead of just the path argument that was passed in. Swagger doesn't just get this information at compilation time because the swagger() function call happens in the middle of building the Elysia app.

Possible solutions:

  • the app swagger() returns passes the full path to the Swagger/Scalar renderer (it already does templating in the handler, so it's probably not a bad idea, might make a PR for this actually)
  • the app swagger() returns has its prefix removed somehow (confusing behavior)
  • the dev tells swagger() what the app prefix is (simple and "good enough," put the prefix in a variable and pass it to both)

@goldenstein64
Copy link

goldenstein64 commented Feb 21, 2025

This issue seems to be fixed by elysiajs/elysia-swagger#185. I think it should be closed.

@wolfenxx
Copy link
Author

thank you @goldenstein64 closing issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants