[Bug][kotlin-spring] add a Spring type converter for enum values #21564 (#21579)

* [kotlin-spring] add a Spring type converter for enum values #21564

* [kotlin-spring] simplify unit test for inner enum converter

* update samples

* code review feedback; move containsEnums to ModelUtils

* code review feedback; provide comment for generated EnumConverterConfiguration.kt

* update samples

---------

Co-authored-by: Chris Gual <cgual@omnidian.com>
This commit is contained in:
Christopher Gual
2025-07-21 03:22:32 -07:00
committed by GitHub
parent 31089c0e49
commit bfb69388aa
9 changed files with 204 additions and 16 deletions

View File

@@ -9,5 +9,6 @@ settings.gradle
src/main/kotlin/org/openapitools/api/ApiUtil.kt
src/main/kotlin/org/openapitools/api/DefaultApi.kt
src/main/kotlin/org/openapitools/api/Exceptions.kt
src/main/kotlin/org/openapitools/configuration/EnumConverterConfiguration.kt
src/main/kotlin/org/openapitools/model/ApiError.kt
src/main/kotlin/org/openapitools/model/ReasonCode.kt

View File

@@ -0,0 +1,26 @@
package org.openapitools.configuration
import org.openapitools.model.ReasonCode
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.convert.converter.Converter
/**
* This class provides Spring Converter beans for the enum models in the OpenAPI specification.
*
* By default, Spring only converts primitive types to enums using Enum::valueOf, which can prevent
* correct conversion if the OpenAPI specification is using an `enumPropertyNaming` other than
* `original` or the specification has an integer enum.
*/
@Configuration(value = "org.openapitools.configuration.enumConverterConfiguration")
class EnumConverterConfiguration {
@Bean(name = ["org.openapitools.configuration.EnumConverterConfiguration.reasonCodeConverter"])
fun reasonCodeConverter(): Converter<kotlin.Int, ReasonCode> {
return object: Converter<kotlin.Int, ReasonCode> {
override fun convert(source: kotlin.Int): ReasonCode = ReasonCode.forValue(source)
}
}
}