OpenAPITools / OpenAPITools/openapi-generator

[BUG][Kotlin][Multiplatform] Incorrect api code when collectionType is list

Open
#5,267 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Kotlin multiplatform client generator is generating incorrect code when collectionType is list. It is generating code that is only appropriate for collectionType of array

openapi-generator version

4.2.3

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: Blah
  version: v2
paths:
  /api/v2/query:
    post:
      operationId: foo
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
Command line used for generation

openapi-generator generate -i test.yml -g kotlin --skip-validate-spec -pcollectionType=list -o output --library multiplatform

Steps to reproduce
  • Store the yml contents into a file named test.yml.
  • execute above command line
  • examine the contents of output/src/commonMain/kotlin/org/openapitools/client/apis/DefaultApi.kt

You will see this generated code that will not compile:

    suspend fun foo(requestBody: kotlin.collections.List<kotlin.String>?) : HttpResponse<kotlin.collections.List<kotlin.String>> {
        val localVariableAuthNames = listOf<String>()

        val localVariableBody = FooRequest(requestBody.asList())

/*-- irrelevant intervening code removed --*/

        return jsonRequest(
            localVariableConfig,
            localVariableBody,
            localVariableAuthNames
        ).wrap<FooResponse>().map { value.toTypedArray() }
    }

The problem here is that the .asList() and .toTypedArray() calls should not be there because requestBody is a list already and the return type is a list not an array.

When collectionType is array those calls are needed because the signature in that case is

    suspend fun foo(requestBody: kotlin.Array<kotlin.String>?) : HttpResponse<kotlin.Array<kotlin.String>> {
Suggest a fix

The template needs to check the collectionType and generate the code appropriately.

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.

Research direction

Reproduce the command using test.yml and inspect output/src/commonMain/kotlin/org/openapitools/client/apis/DefaultApi.kt. Start with the Kotlin multiplatform generator templates that produce the request and response conversions, compare the list and array cases, and verify that generated list code no longer uses asList() or toTypedArray() and compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.