open-api: fails to handle 'multipart/form-data' in openapi v3 (Unsupported Content-Type multipart/form-data; boundary=...)
i’m getting an error trying to handle file uploads with v3.6.0. The spec is:
openapi: "3.0.0"
servers:
- url: http://localhost:3000
description: local server
paths:
/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
# tried this as well but also doesnt work:
# schema:
# type: object
# properties:
# upload:
# type: string
# format: binary
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 or swagger-ui:
curl "http://localhost:3000/upload/" -H "accept: */*" -H "Content-Type: multipart/form-data" -F docs=@package.json
fails with the following error (the endpoint never gets called):
Unsupported Content-Type multipart/form-data; boundary=----WebKitFormBoundaryWyK9kAU7d35AKf26
The same happens with superagent/supertest and postman. Any hints?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 37 (37 by maintainers)
Thanks @mbsimonovic .
In your example, changing the spec like so:
(Note the
required
property.)This causes the following error:
This, I believe, is the crux of our issue!
Edit, to explain further: The reason your spec is validating is because the
files
property is in fact not being validated, because it’s an optional property. From the validator’s point of view, that property doesn’t exist to validate. Making it a required field changes all that. Now you’ll need to putfiles
on thereq.body
beforex-express-openapi-additional-middleware
, i.e. viaargs.consumesMiddleware
. Once that’s done, you’ll get the same error I’ve been getting all along:here you go. so
npm install
first, thennpm run test
.openapi-multipart-demo.zip