Open kkretsch opened 3 years ago
you may add the validator inside osa module. similar as authentication validator, it will always executed before route the request to swagger REST API
In order to add additional middleware before the validation middleware, I have done something similar to what you described. Take a look at the dist/middleware/express.app.config.js file. This is where middleware is added to the app object. I simply added my own parameter to the options object, then modified express.app.config.js to add additional middleware.
For example in index.js:
var options = { openApiValidator: { ... }, routing: { controllers: path.join(__dirname, './controllers'), }, preValidationMiddleware: [ function(req, res, next) { // do something }, function(req, res, next) { // do something else } ] }
Then in express.app.config.js you can access the new parameter in the constructor via the appOptions argument:
class ExpressAppConfig { constructor(definitionPath, appOptions) { .... this.app.use(cookieParser()); // set additional middleware this.setPreValidationMiddleware(appOptions); const swaggerUi = new swagger_ui_1.SwaggerUI(swaggerDoc, appOptions.swaggerUI); this.app.use(swaggerUi.serveStaticContent()); this.app.use(OpenApiValidator.middleware(this.openApiValidatorOptions)); ..... } setPreValidationMiddleware(appOptions) { if('preValidationMiddleware' in appOptions) { appOptions.preValidationMiddleware.forEach(middleware => { this.app.use(middleware); }); } }
This approach does require forking so your modifications don't get clobbered anytime you rebuild the dependencies. However, it's not to difficult to configure.
+1
I would like to fiddle around with the app object to add middleware to be called before the swagger stuff. But as the express app object is created within the oas3 module I could only add at the end of the process queue which will not work. How can I either define my own app object and feed it to the oas3 middleware constructor or add some definition to get to this goal?
PS: I would also try to just fork the module but nodejs module handling is all new to me, so that is too much work for the moment. I cannot even rebuild the unchanged module by now. A how-to-hack this module doc would be great too.