Skip to content

[BUG][kotlin][jvm-spring-restclient] multipart/form-data request is broken #20597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 of 6 tasks
zakupower opened this issue Feb 5, 2025 · 1 comment · Fixed by #20598
Closed
5 of 6 tasks

[BUG][kotlin][jvm-spring-restclient] multipart/form-data request is broken #20597

zakupower opened this issue Feb 5, 2025 · 1 comment · Fixed by #20598

Comments

@zakupower
Copy link
Contributor

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating a kotlin client for a multipart/form-data request with the jvm-spring-restclient library, the api client returns the following error: Content type 'multipart/form-data' not supported for bodyType=java.util.Collections$SingletonMap.

This is because of a bug introduced by #11911 and it was fixed for the okhttp library with this pull request #13435 and it was fixed for jvm-spring-webclient with this pull request #19811.

openapi-generator version

7.11.0

OpenAPI declaration file content or url

Here is a minimal spec for a multipart/form-data file upload.

openapi: '3.0.1'
info:
  version: 1.0.0
  title: Multipart upload request issue
paths:
  /multipart-upload:
    post:
      tags:
        - multipart
      description: Multipart upload request issue
      operationId: multipartUpload
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              required:
                - file
              properties:
                file:
                  description: "a file"
                  type: string
                  format: binary
      responses:
        '200':
          description: Successful operation
Generation Details

kotlin, jvm-spring-restclient

Related issues/PRs

Issue introduced with: #11911
OkHttp fix: #13435
Kotlin jvm-spring-webclient fix: #19811

Suggest a fix

Here is how the ApiClient is generated. Only the relevant method is shown.

open class ApiClient(protected val client: RestClient) {
// [...]
  
    private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>) =
        apply { if (requestConfig.body != null) body(requestConfig.body) }

// [...]
}

Here is how a working solution might look like:

open class ApiClient(protected val client: RestClient) {
// [...]

    private fun <I : Any> RestClient.RequestBodySpec.nullableBody(requestConfig: RequestConfig<I>): RestClient.RequestBodySpec {
        when {
            requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
                val parts = LinkedMultiValueMap<String, Any>()
                (requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
                    if (part.body != null) {
                        parts.add(name, part.body)
                    }
                }
                return apply { body(parts) }
            }

            else -> {
                return apply { if (requestConfig.body != null) body(requestConfig.body) }
            }
        }
    }
// [...]
}

I will prepare a PR with this fix in the following days.

@0xR
Copy link

0xR commented Feb 25, 2025

@wing328 @4brunu Can this change #20597 be released? I want to send formdata, but until this is released I have to rely on a local template override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants