OpenAPITools / OpenAPITools/openapi-generator

[BUG] kotlin-spring generator, different results for enum classes and in-place enums (mustache templates)

Open
#8,442 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.