OpenAPITools / OpenAPITools/openapi-generator
[BUG] kotlin-spring generator, different results for enum classes and in-place enums (mustache templates)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Hi,
I noticed following difference in the generated enum, when using an enum class over an in-place enum in a data class. The difference is already in the mustache Templates:
- Data Class
@JSONProperty is set, so the enum is generated using the value given in the @JSONProperty. Here it is the enum value from the Open Api Spec. See the description for x-enum-varnames - Enum Class
@JSONProperty is not set, so the enum is generated using the enum name
This leads to different behavior in the handling of enums inside the spring application. I fixed it by overwriting the
mustache Template enumClass.mustache as follows:
import com.fasterxml.jackson.annotation.JsonProperty
/**
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
enum class {{classname}}(val value: {{dataType}}) {
{{#allowableValues}}{{#enumVars}}
@JsonProperty({{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}{{/allowableValues}}
}
hope it helps
Kind regards
Jonny
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Compare the Kotlin Spring templates at modules/openapi-generator/src/main/resources/kotlin-spring/dataClass.mustache and enumClass.mustache, starting with their JsonProperty annotations and enumVars handling. Regenerate equivalent enum-class and in-place-enum schemas, then verify both forms use the OpenAPI enum values consistently in the generated Kotlin code.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100