[BUG] `ApiClient.kt.mustache` calls `.toString()` method on a `ByteArray` when building multipart request

Open Beginner friendly
#16,901 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
kotlin
Domain
api

Research direction

Start in modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-okhttp/infrastructure/ApiClient.kt.mustache, especially requestBody(content: T, mediaType: String?) and the multipart addPart call around line 156. Reproduce the issue by generating a Kotlin client from the supplied multipart OpenAPI definition and posting a ByteArray; done means the request contains the byte array contents rather than its object representation.

Written by the indexing model from the issue text.

Description

Issue: Bug
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

In the ApiClient.kt.mustache file during the creation of the request body the requestBody(content: T, mediaType: String?) method correctly builds a multipart/form request when part.body is File, but for all other data types the part is added to the request by calling the .toString() method on the part body:

addPart(
    Headers.of(partHeaders),
    RequestBody.create(null, parameterToString(part.body))
)

This results in the .toString() method being called on a ByteArray - which returns a string containing the class name, '@' symbol, and the hash code of the array object ("[B@7c67645b"), instead of the contents of the byte array.

openapi-generator version

v7.1.0

OpenAPI declaration file content or url
Generation Details
openapi: 3.0.1
info:
  contact:
    email: na@na.com
  description: OpenAPI definitions
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  termsOfService: https://terms.io/
  title: API
  version: 16.0.0
security:
- basicAuth: []
- bearerAuth: []
paths:
  /path/to/resource:
    post:
      operationId: createReceipt
      parameters:
      - in: path
        name: testId
        required: true
        schema:
          type: string
          format: uuid
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: object
Steps to reproduce

From the given api spec, generate a Kotlin Api client and POST a ByteArray in the request.

Related issues/PRs
Suggest a fix
Dominant language
Java
Stars
26.8k
Forks
7.7k
Avg merge
1d 10h
Merged PRs (30d)
103

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.

More from OpenAPITools/openapi-generator

All issues in OpenAPITools/openapi-generator

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.