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

No longer able to add custom routes or middlewares #41

Open
pirumpi opened this issue May 26, 2021 · 3 comments
Open

No longer able to add custom routes or middlewares #41

pirumpi opened this issue May 26, 2021 · 3 comments

Comments

@pirumpi
Copy link

pirumpi commented May 26, 2021

The custom routes are not longer working with the latest update

'use strict';

var path = require('path');
var http = require('http');

var oas3Tools = require('oas3-tools');
var serverPort = 8080;

// swaggerRouter configuration
var options = {
    routing: {
        controllers: path.join(__dirname, './controllers')
    },
};

var expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, 'api/openapi.yaml'), options);
var app = expressAppConfig.getApp();

/** THIS CUSTOM ROUTE IS NOT LONGER ACCESSIBLE **/
app.get('/foo', (req, res) => { res.send('foo')})

// Initialize the Swagger middleware
http.createServer(app).listen(serverPort, function () {
    console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
    console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});
@sunnyofhell
Copy link

sunnyofhell commented Jun 11, 2021

Big issue, causes CORS middleware to not apply. That means the API will fail, from every client.

@akapuya
Copy link

akapuya commented Jun 15, 2021

This is a significant regression, version 2.0.2 supported middleware, it is an important bug to fix

@sunnyofhell
Copy link

Some hacky code to get this to work, till the fix is out

Take the middlewares (4) you added and insert it into the 5th position(arbitrary - you can try any position before router, and after express json middlewares)

const stack = app._router.stack;
const lastEntries = stack.splice(app._router.stack.length - 4);  // The number of middle ware you added 4 in this case. 
const firstEntries = stack.splice(0, 5); // Arbitary number 5, adding our middleware after the first 5
app._router.stack = [...firstEntries, ...lastEntries, ...stack];
console.log(app._router.stack);

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

3 participants