-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
Conversation
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! |
@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! |
V4 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/ |
@mahnunchik thank you for submitting this. I will definitely have a look |
i modified `examples/3-eov-operations, and used and replaced 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 |
@mahnunchik see comment above |
Hi @cdimascio The root of the issue is due to async nature of initialization of 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 I think that |
@cdimascio thanks! |
Excellent! Thanks a lot for trying out the alpha. Super great to have your help |
fix for issue #369
#352