OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
22.01k stars 6.6k forks source link

[BUG] Multipart File upload does not work for generated Ktor clients due to missing content disposition headers #19644

Open JishnuRamesh opened 2 months ago

JishnuRamesh commented 2 months ago

Bug Report Checklist

Description

Multipart file upload using generated client does not work due to missing filename in content disposition headers. I generated a client against an upload API in Kotlin Spring. When using the generated client to upload a file Spring returns status 400 indicating the file has not been uploaded. After a bit of digging I found that Spring is expecting the filename in content disposition but its missing from the generated client.

Actual Output: Returns status 400 due to file missing Expected Output: The file should be uploaded and return a status 200

The issue seemed to resolve when I manually added the headers.

openapi-generator version

7.8.0 ( I did observe this issue in older versions as well.)

OpenAPI declaration file content or url

https://gist.github.com/JishnuRamesh/4aa53186b4725f6daff11aee641e3ee3

Generation Details

The API application is created using spring boot. Code was generated using the below configuration

    tasks.register<org.openapitools.generator.gradle.plugin.tasks.GenerateTask>("generateClient") {
    generatorName.set("kotlin")
    inputSpec.set("$projectDir/swagger.json") 
    outputDir.set("$projectDir/src/main/kotlin")
    apiPackage.set("org.example.api")
    modelPackage.set("org.example.api.model")
    configOptions.set(mapOf("dateLibrary" to "java8"))
    additionalProperties.set(mapOf("java8" to "true"))
    skipOverwrite.set(true)
    verbose.set(true)
    validateSpec.set(false)
    library.set("jvm-ktor")
}
Steps to reproduce
  1. Create a multipart file upload API in Spring
  2. Use the swagger.json to generate a ktor client for the upload API
  3. try uploading a file using the generated client

Spring will respond with 400 with the error required field file is missing. I narrowed down the issue to be that the generated client is missing HTTP Headers ContentDisposition with filename. If I manually add this header then the API call works as expected as shown below

                        formData {
                        file?.apply { append("file", file, Headers.build {
                            append(HttpHeaders.ContentDisposition, "filename=\"file\"") })} // this header missing
                        }
Related issues/PRs

A similar issue was raised here

Files

GeneratedUploadApi.kt.txt GeneratedUploadApiFixed.kt.txt Upload.kt.txt

JishnuRamesh commented 1 week ago

A suggestion for the fix is to modify the Ktor client to also accept a file name when it's a multipart upload. Then, we could pass the filename as one of the body params when it's a multipart upload.