Closed tzuge closed 2 years ago
Yep -makes sense. There is another function serveFiles which doesn’t use swaggerInit however it doesn’t use the swaggerDoc from the request. I will look to make a change and update the docs and let you know.
Released: https://github.com/scottie1984/swagger-ui-express/releases/tag/4.4.0
Close for now, please let me know.
Maybe it works, but as you said it doesn’t use the swaggerDoc from the request. In my case we also need two swagger docs.
So every time that user send request to the server, the middleware is modifying the request again.
let origSwagger = require('./../swagger-output.json');
let origSwagger2 = require('./../swagger-output2.json');
let swaggerDocument = Object.assign({}, origSwagger);
let swaggerDocument2 = Object.assign({}, origSwagger2);
const modifySwaggerByHeader = (doc, origDoc) => (req, res, next) => {
Object.assign(doc, origDoc);
doc.basePath = req.headers["x-base-prefix"] + doc.basePath;
req.swaggerDoc = doc;
next();
}
app.use(config.get("apiUrl") + "/api-docs", modifySwaggerByHeader(swaggerDocument, origSwagger), swaggerUi.serveFiles(swaggerDocument, swaggerUiOptions), swaggerUi.setup())
app.use(config.get("apiUrl") + "/api-docs2", modifySwaggerByHeader(swaggerDocument2, origSwagger2), swaggerUi.serveFiles(swaggerDocument2, swaggerUiOptions), swaggerUi.setup())
In the case from the example, if the x-base-prefix header is "/hello" in the first time the basePath is /hello/api/v1, in the second time is /hello/hello/api/v1. Adding the prefix multiple times.
I solved it using Object.assign to reset the swagger document every time. Any better way to solve it?
Love getting express with swagger going fast with this library.
I think there is a defect or at least documentation gap around dynamic docs in production scenarios.
If I understand correctly,
swagger-ui-init.js
is generated on the first browser request in the handler returned bysetup()
, then returned on a follow-up request due to the script src, and the state is maintained in the closure overswaggerInit
.In a multi-user scenario, user A requests
/api-docs?world=Mars
then user B requests/api-docs?world=Venus
, user B request would overrideswaggerInit
and user A browser might getswagger-ui-init.js
associated with user B request.In a load balanced scenario, user A requests
/api-docs?world=Mars
, but the subsequent request forswagger-ui-init.js
could go to an instance whereswaggerInit
has not been updated.Can the initialization script just be inline with the html returned, or alternatively the swagger ui options could be included in the script src URL for
swagger-ui-init.js
?