-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[Bug][kotlin-spring] add a Spring type converter for enum values #21564 #21579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wing328
merged 7 commits into
OpenAPITools:master
from
btpnlsl:bugfix/21564-enum-converter
Jul 21, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea3fabf
[kotlin-spring] add a Spring type converter for enum values #21564
cgual-omnidian fc8ec1a
[kotlin-spring] simplify unit test for inner enum converter
cgual-omnidian 493d2d0
update samples
cgual-omnidian c01a85b
Merge branch 'OpenAPITools:master' into bugfix/21564-enum-converter
btpnlsl 9a6a0b5
code review feedback; move containsEnums to ModelUtils
cgual-omnidian 64ce1af
code review feedback; provide comment for generated EnumConverterConf…
cgual-omnidian 2af405d
update samples
cgual-omnidian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
modules/openapi-generator/src/main/resources/kotlin-spring/converter.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package {{configPackage}} | ||
|
|
||
| {{#models}} | ||
| {{#model}} | ||
| {{#isEnum}} | ||
| import {{modelPackage}}.{{classname}} | ||
| {{/isEnum}} | ||
| {{/model}} | ||
| {{/models}} | ||
|
|
||
| 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 = "{{configPackage}}.enumConverterConfiguration") | ||
| class EnumConverterConfiguration { | ||
|
|
||
| {{#models}} | ||
| {{#model}} | ||
| {{#isEnum}} | ||
| @Bean(name = ["{{configPackage}}.EnumConverterConfiguration.{{classVarName}}Converter"]) | ||
| fun {{classVarName}}Converter(): Converter<{{{dataType}}}, {{classname}}> { | ||
| return object: Converter<{{{dataType}}}, {{classname}}> { | ||
| override fun convert(source: {{{dataType}}}): {{classname}} = {{classname}}.forValue(source) | ||
| } | ||
| } | ||
| {{/isEnum}} | ||
| {{/model}} | ||
| {{/models}} | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...integer-enum/src/main/kotlin/org/openapitools/configuration/EnumConverterConfiguration.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...equest-model/src/main/kotlin/org/openapitools/configuration/EnumConverterConfiguration.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.openapitools.configuration | ||
|
|
||
| import org.openapitools.model.MultipartMixedStatus | ||
|
|
||
| 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.multipartMixedStatusConverter"]) | ||
| fun multipartMixedStatusConverter(): Converter<kotlin.String, MultipartMixedStatus> { | ||
| return object: Converter<kotlin.String, MultipartMixedStatus> { | ||
| override fun convert(source: kotlin.String): MultipartMixedStatus = MultipartMixedStatus.forValue(source) | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.