Skip to content
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

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

Closed
5 of 6 tasks
martinLeht opened this issue Jun 12, 2024 · 2 comments · Fixed by #18965

Comments

@martinLeht
Copy link

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

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:

  • generateOneOfAnyOfWrappers=true
  • enumPropertyNaming=original
  • serializationLibrary=gson
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: #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
Copy link
Member

wing328 commented Jun 18, 2024

thank for reporting the issue

filed #18965 to fix it

@wing328
Copy link
Member

wing328 commented Jun 20, 2024

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

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

Successfully merging a pull request may close this issue.

2 participants