You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[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
[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`data`.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.
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
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.
The text was updated successfully, but these errors were encountered:
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:
The above template produces uncompilable code, when the variable {{name}} = "data" in the OpenAPI spec. The generated code would look like this (uncompilable):
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:
openapi-generator version
This issue currently on latest 7.7.0-SNAPSHOT version.
OpenAPI declaration file content or url
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: #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.
The text was updated successfully, but these errors were encountered: