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
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:
Install modules npm i
Generate Swagger file npm run swagger-gen
Start API npm run start
The servers does not seem to be computed again according to the new host.
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:
npm i
npm run swagger-gen
npm run start
The
servers
does not seem to be computed again according to the newhost
.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.