apigee-127 / swagger-tools

A Node.js and browser module that provides tooling around Swagger.
MIT License
701 stars 373 forks source link

Swagger 2.0 spec allows array of files #550

Open ziaenezhad opened 6 years ago

ziaenezhad commented 6 years ago

so in this form, maxCount is not necessary https://swagger.io/docs/specification/2-0/file-upload/

googlebot commented 6 years ago

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


uniquejava commented 5 years ago

@whitlockjc Hello, any chance this will be merged, I think upload multiple files is a common use case. I need to upload multiple files. I specified API definition in swagger.yaml as following (the swagger OpenAPI2.0 documentation used this as one example),

in app.js

    let upload = multer({
      storage: multer.memoryStorage(),
      limits: {
        fileSize: 5 * 1024 * 1024
      }
    });

    app.use(upload.fields([{ name: "myfile" }]));

    // install middleware
    swaggerExpress.register(app);

in swagger.yml

      consumes:
        - multipart/form-data
      parameters:
        - name: client_id
          in: query
          description: client id
          type: string
          required: true
        - name: some_field1
          in: formData
          type: string
          required: true
        - name: some_fieild2
          in: formData
          type: string
          required: true
        - name: some_field3
          in: formData
          type: string
          required: true
        - name: myfile
          in: formData
          type: array
          items:
            type: string
            format: binary
          required: true

This pull request fixed my problem. before that, It will always return this error.

{"message":"Validation errors",
"errors":[
{
"code":"INVALID_REQUEST_PARAMETER",
"errors":[{"code":"REQUIRED","message":"Value is required but was not provided","path":["paths","/user","post","parameters","4"]}],
"in":"formData",
"message":"Invalid parameter (myfile): Value is required but was not provided",
"name":"myfile",
"path":["paths","/user","post","parameters","4"]
}
]
}

Using the code from this PR., the upload request performed successfully.

uniquejava commented 5 years ago

Fixed. 😄 the above issues resolved with the following configuration(without this PR)

        - name: myfile
          in: formData
          type: file
          items:
            type: string
            format: binary
          required: true