Skip to content

[BUG] [JS] Multi File Upload is not supported  #7913

@tray2100

Description

@tray2100

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

There doesn't seem to be support for multi file upload in the JS generator. The current implementation only supports a parameter being a single file and not a collection of files.

You can see this here where only

     for (var key in _formParams) {
        if (_formParams.hasOwnProperty(key)) {
          if (this.isFileParam(_formParams[key])) {
            // file field
            request.attach(key, _formParams[key]);
          } else {
            request.field(key, _formParams[key]);
          }
        }
      }

Additionally, the Code Generator defaults collections to the collection format csv which doesn't work for File objects. The more ideal thing would be to let superagent handle it and pass through the incoming collection.

openapi-generator version

4.3.1 ... doesn't look to be a regression. Just missing handling

OpenAPI declaration file content or url

There's already a schema that shows this issue: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/form-multipart-binary-array.yaml

paths:
  /multipart-array:
    post:
      tags:
        - multipart
      description: MultipartFile array test
      operationId: multipartArray
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                files:
                  type: array
                  description: "Many files"
                  items:
                    type: string
                    format: binary
Steps to reproduce

Generate JS code for this schema and you'll see that

Related issues/PRs
Suggest a fix

Add support for adding multiple files as individual parts to the request

     for (var key in _formParams) {
        if (_formParams.hasOwnProperty(key)) {
          let _formParamsValue = _formParams[key];
          if (this.isFileParam(_formParamsValue)) {
            // file field
            request.attach(key, _formParamsValue);
          } else if (Array.isArray(_formParamsValue) && _formParamsValue.length  
            && this.isFileParam(_formParamsValue[0])) {
            // multiple files
            _formParamsValue.forEach(file => request.attach(key, file));
          } else {
            request.field(key, _formParamsValue);
          }
        }
      }

Support collections of files better:

       var formParams = {
         'request': opts['request'],
         'files': this.apiClient.buildCollectionParam(opts['files'], 'passthrough')
       };

where passthrough causes buildCollectionParam to return the input array.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions