OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] IllegalArgumentException when using files with non-ascii chars for multipart requests.

Open
#9,053 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

When using multipart requests the base ApiClient class throws an exception when building the body if the file contains non-ascii chars. A file named ítém.jpg will not be uploaded but item.jpg will work like a charm.

openapi-generator version

5.1.0

OpenAPI declaration file content or url

The bug is not related to any specific OpenAPI declaration file, but I can provide one if you want.

Generation Details

This is the config I'm using:


openApiGenerate {
    generatorName = "java"
    inputSpec = "$rootDir/specs/open-api.yaml".toString()
    outputDir = "$buildDir/generated/api".toString()
    apiPackage = "com.myapp.api"
    invokerPackage = "com.myapp.api.invoker"
    modelPackage = "com.myapp.api.model"
    configOptions = [
            dateLibrary: "legacy"
    ]
    globalProperties = [
            modelDocs: "false"
    ]
}
Steps to reproduce

Generate a Java client using any OpenAPI spec. Send a request using a file containing no-ascii chars and the client will throw an exception.

This is the exception thrown:

Caused by: java.lang.IllegalArgumentException: Unexpected char 0xed at 45 in Content-Disposition value: form-data; name="klisstItemImage"; filename="ítem.jpeg"
        at okhttp3.Headers$Companion.checkValue(Headers.kt:434)
        at okhttp3.Headers$Companion.of(Headers.kt:377)
        at okhttp3.Headers.of(Unknown Source:2)
        at com.klisst.api.invoker.ApiClient.buildRequestBodyMultipart(ApiClient.java:1230)
        at com.klisst.api.invoker.ApiClient.buildRequest(ApiClient.java:1074)
        at com.klisst.api.invoker.ApiClient.buildCall(ApiClient.java:1033)
        at com.klisst.api.DefaultApi.klisstItemUploadImageCall(DefaultApi.java:1579)
        at com.klisst.api.DefaultApi.klisstItemUploadImageValidateBeforeCall(DefaultApi.java:1601)
        at com.klisst.api.DefaultApi.klisstItemUploadImageWithHttpInfo(DefaultApi.java:1648)
        at com.klisst.api.DefaultApi.klisstItemUploadImage(DefaultApi.java:1625)
        at com.klisst.editklisstitem.api.KlisstItemApiClient$addPictureToItem$1.invoke(KlisstItemApiClient.kt:77)
        at com.klisst.editklisstitem.api.KlisstItemApiClient$addPictureToItem$1.invoke(KlisstItemApiClient.kt:76)
        at com.klisst.core.api.BaseApiClient.doEvaluateApiCall(BaseApiClient.kt:47)
        at com.klisst.core.api.BaseApiClient.doEvaluateApiCall$default(BaseApiClient.kt:36) 
        at com.klisst.core.api.BaseApiClient.evaluateApiCall(BaseApiClient.kt:19) 
        at com.klisst.editklisstitem.api.KlisstItemApiClient.addPictureToItem(KlisstItemApiClient.kt:76) 
        at com.klisst.editklisstitem.usecases.EditKlisstItem$uploadAndDeleteImagesAsynchronously$uploadJobs$1$1.invoke(EditKlisstItem.kt:62) 
        at com.klisst.editklisstitem.usecases.EditKlisstItem$uploadAndDeleteImagesAsynchronously$uploadJobs$1$1.invoke(EditKlisstItem.kt:61) 
        at com.klisst.core.coroutines.AsyncAwait$deferred$1.invokeSuspend(AsyncAwait.kt:25) 
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) 
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) 
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) 
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) 
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) 
Related issues/PRs

None AFAIK.

Suggest a fix

The file name should be URL encoded before generating the part header or the non-ascii chars should be removed. ApiClient.java line 1230 should not crash if there are non-asccii chars as part of the file name.

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 in the generated Java ApiClient.java method buildRequestBodyMultipart, at the line identified in the stack trace, and reproduce the request with a filename such as ítém.jpeg. Trace how the multipart Content-Disposition header is built. Done means a generated Java client can send a multipart file whose name contains non-ASCII characters without throwing IllegalArgumentException.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.