Skip to content

[BUG][kotlin][jvm-spring-webclient] multipart/form-data request is broken #19799

@nschimmrich

Description

@nschimmrich

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-webclient 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.

openapi-generator version

7.8.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-webclient

Related issues/PRs

#11911
#13435

Suggest a fix

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

open class ApiClient(protected val client: WebClient) {

// [...]

    private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>) =
        apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }

// [...]

}

Here is how a working solution might look like:

open class ApiClient(protected val client: WebClient) {

// [...]

    private fun <I : Any> WebClient.RequestBodySpec.body(requestConfig: RequestConfig<I>): WebClient.RequestBodySpec {
        when {
            requestConfig.headers[HttpHeaders.CONTENT_TYPE] == MediaType.MULTIPART_FORM_DATA_VALUE -> {
                val builder = MultipartBodyBuilder()
                (requestConfig.body as Map<String, PartConfig<*>>).forEach { (name, part) ->
                    if (part.body != null) {
                        val partHeaders = part.headers.toMutableMap()
                        val partBuilder = builder.part(name, part.body)
                        partHeaders.forEach { partBuilder.header(it.key, it.value) }
                    }
                }
                return apply { bodyValue(builder.build()) }
            }
            else -> {
                return apply { if (requestConfig.body != null) bodyValue(requestConfig.body) }
            }
        }
    }

// [...]

}

I will try to make a PR with this fix in the following days.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions