OpenAPITools / OpenAPITools/openapi-generator
[BUG][KOTLIN] Number-based enums with defaults result in invalid Kotlin 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
Number-based enums with defaults result in invalid Kotlin enums. With no options, enums are generated like this:
enum class OwnedByYouGetArchivedDeals(val value: java.math.BigDecimal) {
@JsonProperty(value = ""0"") _0("0"),
@JsonProperty(value = ""1"") _1("1")
}
After adding number=kotlin.Int as a type mapping, the resulting enum is fine, but the instantiations of non-null default values are still broken:
enum class IsActive(val value: kotlin.Int) {
@JsonProperty(value = "0") _0(0),
@JsonProperty(value = "1") _1(1);
}
val isActive: BaseWebhook.IsActive? = kotlin.Int("IsActive._1"),
openapi-generator version
I use the latest version of the Maven plugin (openapi-generator-maven-plugin, version 7.20.0).
OpenAPI declaration file content or url
Number-based boolean enums, with a default value:
- in: query
name: skip_users
schema:
title: numberBooleanDefault0
type: number
default: 0
enum:
- 0
- 1
description: 'When enabled, the teams will not include IDs of member users'
Generation Details
mvn generate-sources
Steps to reproduce
Generate Kotlin sources based on the OpenAPI specification shown above.
Related issues/PRs
I couldn't find any.
Suggest a fix
The type constructor shouldn't be used for enums.
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
Reproduce the supplied OpenAPI schema through the openapi-generator-maven-plugin by running mvn generate-sources, then inspect the generated Kotlin enum and its default-value initialization. Done means number-based enums and non-null defaults produce valid Kotlin without using the enum type constructor.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100