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
22.02k stars 6.61k forks source link

[BUG][KOTLIN] Enum generation not compiling #14017

Open mikezilla opened 2 years ago

mikezilla commented 2 years ago

Bug Report Checklist

Description

When an enum with the same name as the class name that it resides in is generated, the generator produces an output that does not compile.

openapi-generator version

generator version 6.2.1

OpenAPI declaration file content or url

link to the json gist

Generation Details
generatorName: kotlin
outputDir: generated/src/main/kotlin
library: jvm-okhttp4
inputSpec: modules/openapi-generator/src/test/resources/helloWorld.json
templateDir: modules/openapi-generator/src/main/resources/Java
additionalProperties:
  serializationLibrary: jackson
  apiSuffix: SafeClient
  enumPropertyNaming: PascalCase
  sortModelPropertiesByRequiredFlag: true
  sortParamsByRequiredFlag: true
  packageName: com.hello.world.generated
Steps to reproduce
  1. Generate the source using the provided generation details and schema above
  2. Navigate to the HelloWorldGreeting.kt file
  3. Notice the compilation error on the enum member variable named helloWorldGreeting
    @field:JsonProperty("HelloWorldGreeting")
    val helloWorldGreeting: HelloWorldGreeting.HelloWorldGreeting? = null  // <-- compilation error on qualified enum
  4. removing the class name qualifier works
    @field:JsonProperty("HelloWorldGreeting")
    val helloWorldGreeting: HelloWorldGreeting? = null  // <-- no compilation error on non-qualified enum
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/13496

Suggest a fix

Our theory is that there is an issue with the mustache generation template

tien commented 11 months ago

I'm having this exact same issue

wing328 commented 11 months ago

library: jvm-okhttp4

does the issue only occur with the jvm-okhttp4 library or all libraries as well?

if it only occurs with jvm-okhttp4, we can plot the fix from other libraries' template.

tien commented 11 months ago

I've only tested this with the Kotlin multiplatform template so far.

Thanks for planning to fix this, in the meantime I used this temporary fix by changing this in data_class_opt_var.mustache & data_class_req_var_mustache:

{{classname}}.{{{nameInCamelCase}}}

To:

{{modelPackage}}.{{classname}}.{{{nameInCamelCase}}}
wing328 commented 11 months ago

can you please file a PR with the suggested fix?

let me know if you need any help

tien commented 11 months ago

@wing328 Yes, I have created a PR here: https://github.com/OpenAPITools/openapi-generator/pull/17414 Hope this helps 💪