Skip to content

[BUG][kotlin] Wrong Jackson annotations generated for integer enums #22991

@ulrichw-svg

Description

@ulrichw-svg

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The kotlin generator produces invalid Jackson annotations when the API specification contains an enum of type integer.

Actual result:

data class ApiError (

    @get:JsonProperty("errorCode")
    val errorCode: ApiError.ErrorCode

) {

    /**
     * 
     *
     * Values: _0,_100
     */
    enum class ErrorCode(val value: kotlin.Int) {
        @JsonProperty(value = "0") _0(0),
        @JsonProperty(value = "100") _100(100);
    }

}

Expected result:

data class ApiError(

    @get:JsonProperty("errorCode", required = true) val errorCode: ApiError.ErrorCode
) {

    /**
    * 
    * Values: _0,_100
    */
    enum class ErrorCode(@get:JsonValue val value: kotlin.Int) {

        _0(0),
        _100(100);

        companion object {
            @JvmStatic
            @JsonCreator
            fun forValue(value: kotlin.Int): ErrorCode {
                return values().firstOrNull{it -> it.value == value}
                    ?: throw IllegalArgumentException("Unexpected value '$value' for enum 'ApiError'")
            }
        }
    }

}

In order for Jackson to serialize/deserialize the enum value correctly, it needs the @get:JsonValue annotation on the value field. The expected result is taken from the output of the kotlin-spring generator (server generator) that generates integer enums correctly. The kotlin generator (client generator) however puts @JsonProperty annotations on each enum value, which is not correct.

openapi-generator version

7.19.0

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Test API
  version: 1.1.0
components:
  schemas:
    ApiError:
      required:
        - errorCode
      type: object
      properties:
        errorCode:
          type: integer
          format: int32
          enum:
            - 0
            - 100
          examples:
            - 100
Generation Details

Maven plugin configuration:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-kotlin</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <library>jvm-spring-restclient</library>
                            <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                            <generatorName>kotlin</generatorName>
                            <generateApiTests>false</generateApiTests>
                            <generateModelTests>false</generateModelTests>
                            <apiPackage>test.client</apiPackage>
                            <modelPackage>test.client.model</modelPackage>
                            <output>${project.build.directory}/generated-sources/api/kotlin</output>
                            <additionalProperties>useRuntimeException=true</additionalProperties>
                            <configOptions>
                                <packageName>test.client</packageName>
                                <serializationLibrary>jackson</serializationLibrary>
                                <useSpringBoot3>true</useSpringBoot3>
                                <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
                                <mapFileBinaryToByteArray>true</mapFileBinaryToByteArray>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce
  1. Generate the code using the openApi specification and the openapi-generator-maven-plugin from above.
Related issues/PRs

This is a similar issue which fixed the implementation for the kotlin-spring generator
#19244

Suggest a fix

I would suggest to have a look at the kotlin-spring generator, which generates the code for integer enums correctly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions