OpenAPITools / OpenAPITools/openapi-generator

okhttp-gson and apache-httpclient library (java) cannot upload multipart files with unicode filenames

Open
#11,796 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

okhttp-gson and apache-httpclient are the only two (synchronous) libraries which can send multipart files at all.

okhttp-gson fails with the following error message if the file contains unicode characters:

Unexpected char 0xf6 at 34 in Content-Disposition value: form-data; name="file"; filename="öäåß.txt"
java.lang.IllegalArgumentException: Unexpected char 0xf6 at 34 in Content-Disposition value: form-data; name="file"; filename="öäåß.txt"
	at okhttp3.Headers$Companion.checkValue(Headers.kt:434)

apache-httpclient sends ?s instead.

My openapi-generator version is 5.4

This is the relevant part of the openapi declaration:

       "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "required": [ "file"  ],
                "type": "object",
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary"
                  }

okhttp-gson would work fine if the following lines in ApiClient.java (1293):

                Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"; filename=\"" + file.getName() + "\"");
                MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
                mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType));

are replaced by:

                MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
                mpBuilder.addFormDataPart(param.getKey(), file.getName(), RequestBody.create(file, mediaType));

For reference. Here is the Java spring boot code, which tries to accept the requests:

    @Operation(summary = "Upload a file.")
    @PostMapping(value = "functionX", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public List<ReturnObject> functionX(@RequestPart("file") MultipartFile excelMultipartFile) throws IOException {

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 generated Java ApiClient.java around line 1293 and reproduce the multipart upload using a filename such as öäåß.txt. Compare the okhttp-gson exception and apache-httpclient question marks, then verify the generated clients preserve the Unicode filename and still accept the Spring multipart endpoint shown in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.