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
20.56k stars 6.28k forks source link

[BUG][KOTLIN] Kotlin client generation with generateOneOfAnyOfWrappers=true produces uncompilable code when array field name is "data" in OpenAPI spec #18908

Closed martinLeht closed 1 week ago

martinLeht commented 3 weeks ago

Bug Report Checklist

Description

I noticed some problems in generated code when testing latest fixes regarding the kotlin client generation with generateOneOfAnyOfWrappers. In the template kotlin-client/data_class.mustache on line 252-256 we have the following jsonArray variable declaration and for-loop:

val jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}")
// validate the required field `{{{baseName}}}` (array)
for (i in 0 until jsonArray{{name}}.size()) {
              {{{items.dataType}}}.validateJsonElement(jsonArray{{name}}.get(i))
}

The above template produces uncompilable code, when the variable {{name}} = "data" in the OpenAPI spec. The generated code would look like this (uncompilable):

val jsonArray`data` = jsonObj.getAsJsonArray("data")
// validate the required field `data` (array) 
for (i in 0 until jsonArray`data`.size()) {
             <SOME_MODEL>.validateJsonElement(jsonArray&#x60;data&#x60;.get(i))
}

As seen above, {{name}} is converted after generation to #x60;data` (not compilable). But when the {{name}} = "entities" or something else than "data", it generates code that is compilable and wokring as expected:

val jsonArrayentities = jsonObj.getAsJsonArray("entities")
// validate the required field `entities` (array)
for (i in 0 until jsonArrayentities.size()) {
              <SOME_MODEL>.validateJsonElement(jsonArrayentities.get(i))
}
openapi-generator version

This issue currently on latest 7.7.0-SNAPSHOT version.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: test
  version: 1.0.0
  description: test
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
  - url: https://petstore.swagger.io/v2
paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        '200':
          description: Updated
components:
  schemas:
    Pet:
      type: object
      required:
        - data
        - size
      properties:
        data:
          type: array
          items:
            type: object
            properties:
              firstname:
                type: string
              lastname:
                type: string
        size:
          type: integer
Generation Details

Use latest generator CLI via docker: openapitools/openapi-generator-cli:latest

Use above provided OpenAPI YAML to reproduce (see detailed below).

Configuration options (additional-properties) for Kotlin client generation:

Steps to reproduce

Run kotlin client generation command with latest openapi-generator-cli image against the OpenAPI YAML provided above with above additional properties described above: docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate -i /local/pets.yaml -g kotlin -o /local/generated/kotlin --additional-properties=generateOneOfAnyOfWrappers=true,enumPropertyNaming=original,serializationLibrary=gson

Related issues/PRs

Bug was noticed after fix: https://github.com/OpenAPITools/openapi-generator/pull/18891

Suggest a fix

Kotlin mustache templates should be fixed, so that one can assign any name for an array field in OpenAPI spec. At this moment "data" as field name would not produce usable Kotlin client code.

wing328 commented 2 weeks ago

thank for reporting the issue

filed https://github.com/OpenAPITools/openapi-generator/pull/18965 to fix it

wing328 commented 1 week ago

@martinLeht merged the fix. can you please give it a try with the latest master or snapshot version?