Repository navigation
Description
(Description partially generated with Claude Sonnet 5.5, than edited/extended)
Is your feature request related to a problem? Please describe.
With the kotlin-server generator, generated enums contain only the values defined in the spec. If a payload contains a value that is not part of the spec (for example because a client or upstream service is newer than the spec, or because persisted data was written by a newer version), deserialization fails with an exception. There is no generator-supported way to fall back to a catch-all value.
The enumUnknownDefaultCase option solves exactly this and is already available for the kotlin (client) and kotlin-spring generators, but it is not listed in the config options of kotlin-server, and setting it has no effect on the generated enums.
Describe the solution you'd like
Support enumUnknownDefaultCase in kotlin-server, consistent with the kotlin and kotlin-spring generators:
- Default stays
false, so existing behavior doesn't change. - When
true, every generated enum gets an additional constant (same naming as in the other Kotlin generators). - The constant is annotated so the serialization library can map unknown values to it, e.g.
@JsonEnumDefaultValuefor Jackson (the user still enablesREAD_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUEon their ObjectMapper), and the equivalent for the other serialization libraries the generator supports.
Example (spec):
components:
schemas:
Status:
type: string
description: Order status
enum:
- active
- inactive
- pending_reviewExpected output with enumUnknownDefaultCase=true:
/**
* Order status
* Values: active,inactive,pending_review,UNKNOWN
*/
enum class Status(val value: kotlin.String) {
@JsonProperty("active")
active("active"),
@JsonProperty("inactive")
inactive("inactive"),
@JsonProperty("pending_review")
pending_review("pending_review"),
@JsonEnumDefaultValue
UNKNOWN("__unknown__");
}This way, this would be automatically mapped with Jackson, given that the feature is activated here:
DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE(Optional: allow configuring the name of the fallback constant)
Enum template
As far as I know, the shared codegen code appends an extra enum var named unknown_default_open_api to the enum when the flag is on. If kotlin-server supported the flag, the template would only need a marker on that last entry:
{{#allowableValues}}{{#enumVars}}
{{#enumUnknownDefaultCase}}{{#-last}}@JsonEnumDefaultValue
{{/-last}}{{/enumUnknownDefaultCase}}@JsonProperty({{{value}}})
{{&name}}({{{value}}}){{^-last}},{{/-last}}
{{/enumVars}}{{/allowableValues}}Describe alternatives you've considered
- Overriding the enum template via
templateDirand adding the constant manually. This works, but every user has to maintain a copy of the template. - Declaring the property as
stringin the spec and mapping to a domain enum in application code. This loses the generated type safety. - Adding an
UNKNOWNvalue to the spec itself. This makes it part of the public API contract, so clients can send it and it shows up in the documentation.
Additional context
- openapi-generator version: 7.27.0 (Maven-Plugin)
- Generator / library: kotlin-server / jax-rs
- Serialization library: Jackson 3
- Related:
enumUnknownDefaultCasesupport inkotlinandkotlin-spring
Activity