scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.43k stars 228 forks source link

[BUG] Changing host on the fly before load is not working #368

Open Curs3W4ll opened 8 months ago

Curs3W4ll commented 8 months ago

Hey!

I saw in the doc that it was possible to dynamically set the host of the Swagger config by retrieving it from the req object (link from the doc).

I tested it, but it does not work in my case.
I searched about this problem in the issues and saw this issue which is exactly my problem.

I'm using v5.0.0, you can find a minimal example here: minimal.zip

This example is using the swagger-auto-gen lib to generate the Swagger file, but it does not change anything for Swagger UI (FYI: It generates an OpenAPI v3 file and add a default server).
Steps to reproduce:

The servers does not seem to be computed again according to the new host.

Did I do anything wrong?

Workaround ```typescript (req: Request, _res: Response, next: NextFunction) => { swaggerFile.host = req.get("host"); swaggerFile.servers[0].url = `http://${swaggerFile.host}/api/v1/`; // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-ts-comment // @ts-ignore req.swaggerDoc = swaggerFile; next(); }, ``` Instead of ```typescript (req: Request, _res: Response, next: NextFunction) => { swaggerFile.host = req.get("host"); // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-ts-comment // @ts-ignore req.swaggerDoc = swaggerFile; next(); }, ```

Available if any question.