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
21.3k stars 6.44k forks source link

[BUG] kotlin-spring, spring-boot code generator creates invalid enums #12553

Open mbogner opened 2 years ago

mbogner commented 2 years ago
Description

Generated enum case is unusable:

    enum class RequiredActions(val value: kotlin.String) {

        @JsonProperty("update_user_locale") updateUserLocale("update_user_locale"),
        @JsonProperty("CONFIGURE_TOTP") cONFIGURETOTP("CONFIGURE_TOTP"),
        @JsonProperty("VERIFY_EMAIL") vERIFYEMAIL("VERIFY_EMAIL"),
        @JsonProperty("UPDATE_PASSWORD") uPDATEPASSWORD("UPDATE_PASSWORD"),
        @JsonProperty("UPDATE_PROFILE") uPDATEPROFILE("UPDATE_PROFILE")
    }

Using RequiredActions.valueOf will of course fail when trying to translate something like CONFIGURE_TOTP to cONFIGURETOTP.

openapi-generator version

6.0.0

OpenAPI declaration file content or url

in an object as property:

        requiredActions:
          type: array
          items:
            type: string
            enum:
              - update_user_locale
              - CONFIGURE_TOTP
              - VERIFY_EMAIL
              - UPDATE_PASSWORD
              - UPDATE_PROFILE
Generation Details
openApiGenerate {
    // https://openapi-generator.tech/docs/generators/kotlin-spring
    generatorName.set("kotlin-spring")
    library.set("spring-boot")

    inputSpec.set(openAPISpecFile)
    outputDir.set("$buildDir/$openAPIGenOutBase")

    packageName.set(openAPIBasePackage)
    apiPackage.set("$openAPIBasePackage.api")
    invokerPackage.set("$openAPIBasePackage.client")
    modelPackage.set("$openAPIBasePackage.model")

    modelNameSuffix.set("Dto")
    typeMappings.set(
        mapOf(
            "DateTime" to "Instant"
        )
    )
    importMappings.set(
        mapOf(
            "Instant" to "java.time.Instant"
        )
    )
    configOptions.set(
        mapOf(
            "useBeanValidation" to "true",
            "delegatePattern" to "true",
            "useTags" to "true",
            "exceptionHandler" to "false",
            "gradleBuildFile" to "false",
            "reactive" to "false",
            "serviceInterface" to "false",
            "sortModelPropertiesByRequiredFlag" to "true",
            "sortParamsByRequiredFlag" to "true",
            "swaggerAnnotations" to "true",
        )
    )
}
Steps to reproduce
Suggest a fix

Keep upper/lower case as it is.

orolhawion commented 8 months ago

I have a similar issue with a string based enum generation. My expectation is that the generated @JsonProperty("<value>") should not contain the value but the var name, shouldn't it?

My enum definition looks basically like this:

MyEnum:
  type: string
  enum:
    - eins
    - zwei
  x-enum-varnames:
    - one
    - two

and generation results in this: @JsonProperty("eins") ONE("eins"), [...]

where I would have expected @JsonProperty("ONE") ONE("eins"), [...]