Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using predefined express app object #35

Open
kkretsch opened this issue Mar 11, 2021 · 3 comments
Open

Using predefined express app object #35

kkretsch opened this issue Mar 11, 2021 · 3 comments

Comments

@kkretsch
Copy link

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.

@liang-faan
Copy link

you may add the validator inside osa module.
similar as authentication validator, it will always executed before route the request to swagger REST API

@camclark85
Copy link

camclark85 commented Mar 17, 2021

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.

@akapuya
Copy link

akapuya commented Jun 15, 2021

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants