OpenAPITools / OpenAPITools/openapi-generator
[BUG][kotlin] Wrong Jackson annotations generated for integer enums
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
- 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
https://github.com/OpenAPITools/openapi-generator/issues/19244
Suggest a fix
I would suggest to have a look at the kotlin-spring generator, which generates the code for integer enums correctly.
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
Start by reproducing the output with the provided OpenAPI declaration and Maven plugin using generatorName kotlin and Jackson serialization. Compare the kotlin generator with the kotlin-spring implementation referenced by issue 19244. Done means integer enums serialize and deserialize through the enum value with the expected Jackson annotations rather than per-member JsonProperty annotations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 58/100