Skip to content

[develop 2.0] add support for multiple file uploads #823

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

Closed
fehguy opened this issue Jan 13, 2015 · 9 comments
Closed

[develop 2.0] add support for multiple file uploads #823

fehguy opened this issue Jan 13, 2015 · 9 comments
Milestone

Comments

@fehguy
Copy link
Contributor

fehguy commented Jan 13, 2015

Per this discussion:

https://groups.google.com/forum/#!topic/swagger-swaggersocket/6a_tAGtj7CE

Would be helpful to add support for arrays of files. This would require updates in swagger-js as well.

@webron
Copy link
Contributor

webron commented Jan 13, 2015

I think there's some confusion here. @mhdl64 is using Swagger 1.2 and not 2.0.

Another problem is that I can't think of a way to describe multiple file uploads, unless we modify the 2.0 spec.

You can describe a single file parameter, no problem. But you cannot specify an array of files, since file is not an applicable value for item's type - https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#itemsType.

We could change that, though it'll carry complications to both the documentation and the schema (not a reason not to add support for it).

Even if we do add support for it, we may need to impose a collectionFormat value of multi. It makes sense for multipart/form-data but possibly a bit less for application/x-www-form-urlencoded (though we can infer the meaning). We can say that the collectionFormat is inferred and with that combination it is assumed to be multi (and cannot be overridden - validation error), but we have a similar case in the spec where we currently force people to include a property with a known default value which cannot be changed.

Last thing, there's a ticket for a future version of the spec to allow specifying the mime type(s) of the uploaded files, and it should integrate well into this solution.

@fehguy
Copy link
Contributor Author

fehguy commented Jan 28, 2015

{
  "type": "array", 
  "items": {
    "type": "file"
  }
}

looks appropriate but it's not supported. See here:
OAI/OpenAPI-Specification#254

@fehguy fehguy added this to the future milestone Jan 28, 2015
@JanDintel
Copy link

@webron @fehguy I was able to work around the issue using a different name for the parameter object. So that the parameter is 'translated' to an Array.

Example:

The parameter files is an Array with uploaded files. Instead of using the parameter name "files", I used the parameter name "files[]". Thus resulting the following Swagger API specification:

{
  "path": "api/v1/upload",
  "operations": [
    {
      "summary": "Upload multiple files",
      "consumes": "multipart/form-data",
      "parameters": [
        {
          "paramType": "formData",
          "name": "files[]",
          "type": "file",
          "required": true
        },
        {
          "paramType": "formData",
          "name": "files[]",
          "type": "file",
          "required": false
        },
        {
          "paramType": "formData",
          "name": "files[]",
          "type": "file",
          "required": false
        },
        {
          "paramType": "formData",
          "name": "files[]",
          "type": "file",
          "required": false
        }
      ],
      "responseMessages": [
        {
          "code": 201,
          "responseModel": null,
          "message": "Created"
        },
        {
          "code": 422,
          "responseModel": null,
          "message": "Unprocessable Entity"
        }
      ],
      "method": "post",
      "nickname": "Api::V1::Uploads#create"
    }
  ]
}

Note:

I'm using a Rails backend and documented the API with the DSL of the swagger-docs gem.

@webron
Copy link
Contributor

webron commented Apr 9, 2015

Technically, that's not a valid Swagger definition though.

@bytesandwich
Copy link

Maybe this could just be "allowMultiple" for files if the param is a form and ignore the param name?
If you're uploading an arbitrary number of files, I'm having a hard time seeing how you could give the notion of multiple files some sort of sane parameter name that is at all intuitive.

I'm just saying because my attempt to get multiple files to work was the same as Muhammad's from the google group post, so maybe other people are going to try the same thing first.

  @ApiOperation(
    httpMethod = "POST",
    nickname = "upload",
    value = "upload images")
  @ApiImplicitParams(
    Array(new ApiImplicitParam(
      name = "picture", // ignore this
      dataType = "file",
      paramType = "form",
      allowMultiple = true,
      required = true),

@webron
Copy link
Contributor

webron commented Apr 10, 2015

Unfortunately, the current specification doesn't provide a way to solve it unfortunately. This is why there's an issue on swagger-spec about it linked above. allowMultiple won't work in this case.

@fehguy
Copy link
Contributor Author

fehguy commented May 8, 2015

ok since this isn't supported by the spec, I'm going to close it.

@fehguy fehguy closed this as completed May 8, 2015
@webermax
Copy link

@webron i assume @JanDintel's suggestion is not a valid definition because he repeats the same parameter name multiple times?

but there should be no problem to only use a single block

{
          "paramType": "formData",
          "name": "files[]",
          "type": "file",
          "required": true
}

only drawback i noticed so far is the file upload in the swagger gui does not support multiple files.

@webron
Copy link
Contributor

webron commented Mar 21, 2016

@webermax the spec doesn't support a definition of a mulit-file parameter. You can define as many file parameters as you want, but you'd have to define the exact number of files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants