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

implement as standard connect middleware #351

Merged
merged 52 commits into from
Sep 28, 2020
Merged

implement as standard connect middleware #351

merged 52 commits into from
Sep 28, 2020

Conversation

cdimascio
Copy link
Owner

@cdimascio cdimascio commented Aug 14, 2020

@cdimascio cdimascio changed the title implement as standard express middleware implement as standard connect middleware Aug 17, 2020
@Envek
Copy link

Envek commented Aug 18, 2020

I found this repo today and decided to try v4.0.0.alpha1 because I liked middleware usage much more than promise-based one. And it works very well for my simple case. Thank you for express-openapi-validator!

@cdimascio
Copy link
Owner Author

@Envek awesome and a huge thank you for posting your feedback. Glad to hear it's working for your use case. Please post any issues that you run into. Thanks again!

@mahnunchik
Copy link

mahnunchik commented Sep 1, 2020

V4 operationHandlers doesn't supports express.Router().

Works as expected:

const app = express();

const api = express();
api.use(OpenApiValidator.middleware({
  operationHandlers: path.join(__dirname),
}));

app.use(api);

Doesn't works - every route is 404 error:

const app = express();

const api = express.Router();
api.use(OpenApiValidator.middleware({
  operationHandlers: path.join(__dirname),
}));

app.use('/api/', api);
// OR even
app.use(api);

Both option uses the same spec with:

servers:
  - url: /api/

@cdimascio
Copy link
Owner Author

@mahnunchik thank you for submitting this. I will definitely have a look

@cdimascio
Copy link
Owner Author

i modified `examples/3-eov-operations, and used

and replaced app.js with the following contents

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const logger = require('morgan');
const http = require('http');
const OpenApiValidator = require('express-openapi-validator');

const port = 3000;
const app = express();
const apiSpec = path.join(__dirname, 'api.yaml');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.text());
app.use(bodyParser.json());

app.use(logger('dev'));
app.use('/spec', express.static(apiSpec));

// 1. Create a router
const api = express.Router();  

// 2. Use oav middleware with router
api.use(
  OpenApiValidator.middleware({
    apiSpec,
    validateResponses: true,
    operationHandlers: path.join(__dirname),
  }),
);

// use the router on the app
app.use('/v1', api);   

app.use((err, req, res, next) => {
  // format errors
  res.status(err.status || 500).json({
    message: err.message,
    errors: err.errors,
  });
});

http.createServer(app).listen(port);
console.log(`Listening on port ${port}`);

module.exports = app;

I then curl'ed an endpoint. It is works

curl 'localhost:3000/v1/pets?type=cat&limit=' 
{"message":"Empty value found for query parameter 'limit'","errors":[{"path":".query.limit","message":"Empty value found for query parameter 'limit'"}]}%   

Would you mind testing out something similar.

Also, please create a github repo with your failing example. i'll have a look.

Do note that you will need the body parser middleware...though likely not the cause of the 404

@cdimascio
Copy link
Owner Author

@mahnunchik see comment above

@mahnunchik
Copy link

Hi @cdimascio

The root of the issue is due to async nature of initialization of OpenApiValidator.

My code suddenly includes the following 404 handler:

api.use(
  OpenApiValidator.middleware({
    apiSpec,
    validateResponses: true,
    operationHandlers: path.join(__dirname),
  }),
);

app.use('/v1', api);   

// actually any handler after API handler
app.use((req, res, next) => {
  res.status(404).send({ error: 'Page not found.' });
});

In this example * -> 404 handler will be added before any routes from OpenApiValidator due to async spec loader.

I think that OpenApiValidator.middleware should loads synchronously to mitigate unpredictable issues related to order of route handlers.

@mahnunchik
Copy link

@cdimascio thanks! v4.0.0-alpha.5 works with mounted Router as expected.

@cdimascio
Copy link
Owner Author

Excellent! Thanks a lot for trying out the alpha. Super great to have your help

@cdimascio cdimascio merged commit b806dd4 into master Sep 28, 2020
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

Successfully merging this pull request may close these issues.

4 participants