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)

Most upvoted comments

Thanks @mbsimonovic .

In your example, changing the spec like so:

multipart/form-data:
    schema:
        required:
            - files
        type: object
        properties:
            files:
                type: array
                items:
                    type: string
                    format: binary

(Note the required property.)

This causes the following error:

errors: [
    {
      "path": "files",
      "errorCode": "required.openapi.validation",
      "message": "should have required property 'files'",
      "location": "body"
    }
  ]

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 put files on the req.body before x-express-openapi-additional-middleware, i.e. via args.consumesMiddleware. Once that’s done, you’ll get the same error I’ve been getting all along:

  errors: [
    {
      "path": "files[0]",
      "errorCode": "type.openapi.validation",
      "message": "should be string",
      "location": "body"
    }
  ]

here you go. so npm install first, then npm run test.

openapi-multipart-demo.zip