OpenAPITools / OpenAPITools/openapi-generator
okhttp-gson and apache-httpclient library (java) cannot upload multipart files with unicode filenames
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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