Comments (16)
I got it to work with multer
and a little tweak. Hopefully the following examples will be helpful for some people.
Multer is a multipart form-data handling middleware. by default it stores the upload to req.file
or req.files
. The express-openapi-validation logic is looking for defined files in req.body
. So we need to copy the file upload to the expected position.
expressOpenapi.initialize({
consumesMiddleware: {
// example 1: Allow for single file upload (filename: 'file')
'multipart/form-data'(req, res, next) {
multer().single('file')(req, res, (err) => {
if (err) return next(err);
req.body.file = req.file;
next();
});
}
// example 2: Allow for any file upload
'multipart/form-data'(req, res, next) {
multer().any()(req, res, (err) => {
if (err) return next(err);
req.files.forEach(f => req.body[f.fieldname] = f);
next();
});
}
},
});
from open-api.
@hkors there's currently no support for file uploads. It would be great if you could propose a solution. Most swagger frameworks that I've seen don't handle this very well. I'd love express-openapi
to be one of the first!
from open-api.
I can now receive the file by adding 'x-express-openapi-disable-validation-middleware': true
to this documentation, but I suppose this shouldn't be the solution...
from open-api.
Hi!
The issue should be fixed now, I had the same problem.
See #55
from open-api.
Thanks @mghignet ! I've published #55 as v0.30.0
. If you have the time, could you add a sample project to the test suite showing a file upload example?
from open-api.
@hkors if you're able to try again with the latest version it would be appreciated. I'll leave this issue open until we have confirmation that file uploads work.
from open-api.
Actually the bug is still present.
The issue is that we need to validate the presence of some fields (typically, a file) in formData, but it's not as easy as for a body or a query string.
The only workaround I've found is to remove the "required" property from formData parameters.
from open-api.
kogosoftwarellc/express-openapi-validation@0096f84
I've commented on this.
The "quick and dirty" solution I see would be to remove the piece of code I commented to disable errors for formData fields, since we're not able to validate them correctly.
It means "required" property (and maybe some other ones) would be ineffective.
What do you think?
from open-api.
@mghignet sorry for the late reply. Holidays and election happened here in the states. Barely getting back in the swing of things.
Whatever we need to do to get file uploads working, as long as the tests pass, I say we do it! Can you submit a PR?
from open-api.
Nice @alrik ! Can you by chance update the README with this example, and possibly create a test (i.e. can you add multer
as a devDependency
to show that it works)?
from open-api.
Hey @jsdevel sure. I'll add an example to the readme and add some tests when i got some spare time. Could you assign me the issue, so that i don't forget about it?
from open-api.
@alrik looks like I'm unable to assign to non-project collaborators. If you own an issue, I can assign it to you. Want to open a new issue here about a missing example in the README for file uploads? I should be able to assign that to you.
from open-api.
I submit that this issue is addressed satisfactorily by #105
from open-api.
Agreed. Thanks @simpgeek! @hkors let us know if you disagree.
from open-api.
i'm getting an error trying to test this with curl. The spec is:
/upload/:
post:
summary: Create NEW files
operationId: upload
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
docs:
type: array
items:
type: string
format: binary
and express-openapi is initialised as suggested:
'multipart/form-data'(req, res, next) {
multer().any()(req, res, (err) => {
if (err) return next(err);
req.files.forEach(f => req.body[f.fieldname] = f);
return next(); // this executes correctly
});
}
Hitting the app with curl:
curl "http://localhost:3000/upload/" -H "accept: */*" -H "Content-Type: multipart/form-data" -F [email protected]
, fails with the following error (the endpoint never gets called):
Unsupported Content-Type multipart/form-data; boundary=------------------------2a07fe1e704ec1f5
The same happens with superagent/supertest and postman. Any hints?
from open-api.
@mbsimonovic please file a new issue. that appears to be related to V3 which wasn't published at the time this issue was created.
from open-api.
Related Issues (20)
- express-openapi: Unclear how to validate successful completion of tutorial HOT 5
- pnpm and glob issue in express-openapi
- Add application/json content type support in openapi-request-coercer
- Add support for `prefixItems` HOT 3
- SyntaxError: Invalid regular expression during path-to-regexp conversion HOT 4
- openapi-framework: fixed windows path names HOT 4
- [enhancement] openapi-response-validator: native ESM node modules default export not working as intended HOT 3
- [ft. request] openapi-response-validator - allow access to response param in errorTransformer HOT 1
- Using automatic paths with ESM HOT 2
- OpenAPI Types package, Path Item Object HOT 1
- Supertest integration tests fail after update to express-openapi 12.x.x
- parameters from Swagger 2.0 (openapi 2.0) YAML file are not caught by express-openapi HOT 2
- parameters from Swagger 2.0 (openapi 2.0) YAML file are not caught by express-openapi
- External refs not captured in parameters for request validation HOT 1
- Bug: OpenAPIResponseValidatorArgs responses object incorrectly defined
- Support for Asynchronous Custom Keywords in openapi-request-validator
- Support for validating OpenAPI 3.1
- Type narrowing
- [Bug] Custom formats not working for objects
- fs-routes is using a vulnerable version of glob HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from open-api.