swagger-api / swagger-api/swagger-codegen

[JAVA] [Swift5] multipart/form-data upload with multiple files in one field

Open
#9,839 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The official Swagger 2.0 documentation states binary string arrays as the workaround for specifying a multipart/form-data upload with multiple files in one field (like a file array).

However, uploading an arbitrary number of files (an array of files) is not supported. There is an open feature request at https://github.com/OAI/OpenAPI-Specification/issues/254. For now, you can use a binary string array as a workaround for uploading an arbitrary number of files:

type: array
items:
  type: string
  format: binary

Swagger UI already has problems correctly rendering this type, but code generation (for Java okhttp-gson and Swift5 at least) "fails" as well. The code generation itself is done without errors, but the request isn't formed correctly.

Swagger-codegen version

2.4.9

Swagger declaration file content or url
parameters:
  - in: formData
    name: images
    description: Image files for the post
    type: array
    items:
      type: string
      format: binary
Command line used for generation

Java (okhttp-gson): java -jar swagger-codegen-cli.jar generate -DhideGenerationTimestamp=true -DartifactId="android-client" -DartifactVersion="0.1" -i openApi.yml -l java --library=okhttp-gson -o build/
Swift5: java -jar swagger-codegen-cli.jar generate -i openApi.yml -l swift5 -o build/

Steps to reproduce
  1. Create a Swagger 2.0 document with a POST endpoint and the parameter provided above.
  2. Run the code generation with one of the commands from above.
  3. Take a look at the generated methods for the upload and/or try to send a file (a simple [mocked] backend should suffice).
Related issues/PRs

https://github.com/OAI/OpenAPI-Specification/issues/254
https://github.com/swagger-api/swagger-codegen-generators/issues/150

PR: #9840

Suggest a fix/enhancement

The codegen templates for Java with okhttp-gson and Swift5 (Alamofire) should interpret arrays with binary strings as file parameters. Like swagger-codegen for the normal Java client does.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Java ApiClient.mustache reference and the codegen templates for Java okhttp-gson and Swift5 Alamofire. Reproduce the upload using the provided Swagger document and generation commands, then inspect the generated methods. Done means multipart requests correctly include multiple binary files in the images field for both generators.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, swift
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.