Closed rfarris closed 2 years ago
I can confirm that this works.
const swaggerUi = require('swagger-ui-express');
...
const swaggerSetupMiddleware = swaggerUi.setup(this.swaggerSpec);
swaggerSetupMiddleware({}, { send: () => {} }, function(err: any): any {
console.log(err);
});
but I have no freakin clue how to write the request / response in typescript :-/
import swaggerUi as * from 'swagger-ui-express'
const swaggerSetupMiddleware = swaggerUi.setup(this.swaggerSpec);
swaggerSetupMiddleware({}, { send: () => {} }, function(err: any): any {
console.log(err);
});
As of about 2 months ago when I last looked at them, the Typescript types for this package were broken machine generated garbage that prevented setting any options on setup
. Unless someone's started maintaining them, they probably shouldn't be used.
As of about 2 months ago when I last looked at them, the Typescript types for this package were broken machine generated garbage that prevented setting any options on
setup
. Unless someone's started maintaining them, they probably shouldn't be used.
Lol yeah that‘s correct the types are useless. Comes from the express serve static core package:
https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/express-serve-static-core
Thanks for the workaround, the way you write it in Typescript is:
const swaggerUIMiddleware = SwaggerUI.setup(openApiDocumentation);
const req: any = {};
const res: any = { send: () => {} };
// Make a mock request to the swagger ui middleware to initialize it.
// Workaround issue: https://github.com/scottie1984/swagger-ui-express/issues/178
swaggerUIMiddleware(req, res, () => {});
app.use('/api-docs', SwaggerUI.serve, swaggerUIMiddleware);
@robgraeber aaaaaaaah ;) thanks - works ! 🥇
Thank you for opening up this issue! Jesus Christ, I spent two days trying to figure this out and finally had the thought that it might be a typescript issue.
Thanks @robgraeber @pharindoko
Hitting this bug in our apps too. @scottie1984 it would be great if you can fix this in swagger-ui-express
so consumers don't have to workaround this in every app...
Halo, I attempted to fix the problem with this PR here https://github.com/scottie1984/swagger-ui-express/pull/190. The idea is to scope the swaggerInit
instead of using it globally, hence remove the need of /swagger-ui-init.js
Thank you so much!!
Please review this #228 and let me know if it fixes.
I will close this as I reverted the code that was causing this issue.
Issue:
Request to
/swagger-ui-init.js
randomly returns no data. It is really bad after a deploy, then gets better over time. Frequency of the issue depends on the size of cluster being load balanced.Setup
Cause The
swaggerInit
global variable used inindex.js
does not get initialized until a request hits the server that goes all the way through the middleware chain and hits theswaggerUi.setup(..)
middleware.Repo Can be trivially reproduced by simply directly requesting the
/swagger-ui-init.js
url after starting up node.or
swaggerUi.serve
and will continue the middleware chain until callingswaggerUi.setup(...)
. This returns the HTML and sets theswaggerInit
global variable.swagger-ui-init.js
goes to Server 2.swaggerUi.serve
matches the url/swagger-ui-init.js
and returns the uninitializedswaggerInit
global variable.Workaround Force initialization at startup using a fake request and response.