Skip to content

Commit 37afe57

Browse files
authored
[Kotlin][Spring] fix #19244 integer enum (#19248)
* [Kotlin][Spring] fix #19244 integer enum * fix embedded array enum
1 parent c93ec54 commit 37afe57

File tree

48 files changed

+912
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+912
-132
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
generatorName: kotlin-spring
2+
outputDir: samples/server/petstore/kotlin-springboot-integer-enum
3+
library: spring-boot
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue19244_integer_enum.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/kotlin-spring
6+
additionalProperties:
7+
interfaceOnly: "true"
8+
skipDefaultInterface: "true"
9+
useTags: "true"
10+
useSpringBoot3: "true"
11+
annotationLibrary: none
12+
documentationProvider: none
13+
enumPropertyNaming: UPPERCASE

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinSpringServerCodegen.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
815815
model.imports.add("JsonProperty");
816816
if (Boolean.TRUE.equals(model.hasEnums)) {
817817
model.imports.add("JsonValue");
818+
model.imports.add("JsonCreator");
818819
}
819820
} else {
820821
//Needed imports for Jackson's JsonCreator
@@ -840,10 +841,14 @@ public ModelsMap postProcessModelsEnum(ModelsMap objs) {
840841
.filter(cm -> Boolean.TRUE.equals(cm.isEnum) && cm.allowableValues != null)
841842
.forEach(cm -> {
842843
cm.imports.add(importMapping.get("JsonValue"));
844+
cm.imports.add(importMapping.get("JsonCreator"));
843845
cm.imports.add(importMapping.get("JsonProperty"));
844846
Map<String, String> itemJsonValue = new HashMap<>();
845847
itemJsonValue.put("import", importMapping.get("JsonValue"));
846848
imports.add(itemJsonValue);
849+
Map<String, String> itemJsonCreator = new HashMap<>();
850+
itemJsonCreator.put("import", importMapping.get("JsonCreator"));
851+
imports.add(itemJsonCreator);
847852
Map<String, String> itemJsonProperty = new HashMap<>();
848853
itemJsonProperty.put("import", importMapping.get("JsonProperty"));
849854
imports.add(itemJsonProperty);

modules/openapi-generator/src/main/resources/kotlin-spring/dataClass.mustache

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@
3131
* {{{description}}}
3232
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
3333
*/
34-
enum class {{{nameInPascalCase}}}(val value: {{#isContainer}}{{#items}}{{{dataType}}}{{/items}}{{/isContainer}}{{^isContainer}}{{{dataType}}}{{/isContainer}}) {
34+
enum class {{{nameInPascalCase}}}(@get:JsonValue val value: {{#isContainer}}{{#items}}{{{dataType}}}{{/items}}{{/isContainer}}{{^isContainer}}{{{dataType}}}{{/isContainer}}) {
3535
{{#allowableValues}}{{#enumVars}}
36-
@JsonProperty({{{value}}}) {{{name}}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
36+
{{{name}}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}};
37+
38+
companion object {
39+
@JvmStatic
40+
@JsonCreator
41+
fun forValue(value: {{#isContainer}}{{#items}}{{{dataType}}}{{/items}}{{/isContainer}}{{^isContainer}}{{{dataType}}}{{/isContainer}}): {{{nameInPascalCase}}} {
42+
return values().first{it -> it.value == value}
43+
}
44+
}
3745
}
3846
{{/isEnum}}{{/vars}}{{/hasEnums}}
3947
{{#serializableModel}}

modules/openapi-generator/src/main/resources/kotlin-spring/enumClass.mustache

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
* {{{description}}}
33
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
44
*/
5-
enum class {{classname}}(val value: {{dataType}}) {
5+
enum class {{classname}}(@get:JsonValue val value: {{dataType}}) {
66
{{#allowableValues}}{{#enumVars}}
7-
@JsonProperty({{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
7+
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}};
8+
9+
companion object {
10+
@JvmStatic
11+
@JsonCreator
12+
fun forValue(value: {{dataType}}): {{classname}} {
13+
return values().first{it -> it.value == value}
14+
}
15+
}
816
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
openapi: 3.0.3
2+
info:
3+
description: >-
4+
Example created
5+
version: 1.0.0
6+
title: OpenAPI Stuff API created to reproduce issue
7+
license:
8+
name: Apache-2.0
9+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
10+
paths:
11+
/healthcheck:
12+
get:
13+
summary: Health check endpoint.
14+
operationId: healthcheck
15+
responses:
16+
204:
17+
description: Successful health check
18+
default:
19+
description: Unexpected error
20+
content:
21+
'application/json':
22+
schema:
23+
$ref: '#/components/schemas/ApiError'
24+
25+
components:
26+
schemas:
27+
ApiError:
28+
required:
29+
- errorCode
30+
type: object
31+
properties:
32+
errorCode:
33+
type: integer
34+
enum:
35+
- 0
36+
- 100
37+
x-enum-varnames:
38+
- OK
39+
- ERROR
40+
reasonCode:
41+
$ref: '#/components/schemas/ReasonCode'
42+
ReasonCode:
43+
type: integer
44+
enum:
45+
- 10
46+
- 20

samples/server/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/model/Order.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.openapitools.model
22

33
import java.util.Objects
4+
import com.fasterxml.jackson.annotation.JsonCreator
45
import com.fasterxml.jackson.annotation.JsonProperty
56
import com.fasterxml.jackson.annotation.JsonValue
67
import java.io.Serializable
@@ -42,11 +43,19 @@ data class Order(
4243
* Order Status
4344
* Values: placed,approved,delivered
4445
*/
45-
enum class Status(val value: kotlin.String) {
46+
enum class Status(@get:JsonValue val value: kotlin.String) {
4647

47-
@JsonProperty("placed") placed("placed"),
48-
@JsonProperty("approved") approved("approved"),
49-
@JsonProperty("delivered") delivered("delivered")
48+
placed("placed"),
49+
approved("approved"),
50+
delivered("delivered");
51+
52+
companion object {
53+
@JvmStatic
54+
@JsonCreator
55+
fun forValue(value: kotlin.String): Status {
56+
return values().first{it -> it.value == value}
57+
}
58+
}
5059
}
5160

5261
companion object {

samples/server/petstore/kotlin-spring-cloud/src/main/kotlin/org/openapitools/model/Pet.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.openapitools.model
22

33
import java.util.Objects
4+
import com.fasterxml.jackson.annotation.JsonCreator
45
import com.fasterxml.jackson.annotation.JsonProperty
56
import com.fasterxml.jackson.annotation.JsonValue
67
import org.openapitools.model.Category
@@ -47,11 +48,19 @@ data class Pet(
4748
* pet status in the store
4849
* Values: available,pending,sold
4950
*/
50-
enum class Status(val value: kotlin.String) {
51+
enum class Status(@get:JsonValue val value: kotlin.String) {
5152

52-
@JsonProperty("available") available("available"),
53-
@JsonProperty("pending") pending("pending"),
54-
@JsonProperty("sold") sold("sold")
53+
available("available"),
54+
pending("pending"),
55+
sold("sold");
56+
57+
companion object {
58+
@JvmStatic
59+
@JsonCreator
60+
fun forValue(value: kotlin.String): Status {
61+
return values().first{it -> it.value == value}
62+
}
63+
}
5564
}
5665

5766
companion object {

samples/server/petstore/kotlin-spring-default/src/main/kotlin/org/openapitools/model/AnyOfUserOrPet.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.openapitools.model
22

33
import java.util.Objects
4+
import com.fasterxml.jackson.annotation.JsonCreator
45
import com.fasterxml.jackson.annotation.JsonProperty
56
import com.fasterxml.jackson.annotation.JsonValue
67
import org.openapitools.model.Category
@@ -83,11 +84,19 @@ data class AnyOfUserOrPet(
8384
* pet status in the store
8485
* Values: available,pending,sold
8586
*/
86-
enum class Status(val value: kotlin.String) {
87-
88-
@JsonProperty("available") available("available"),
89-
@JsonProperty("pending") pending("pending"),
90-
@JsonProperty("sold") sold("sold")
87+
enum class Status(@get:JsonValue val value: kotlin.String) {
88+
89+
available("available"),
90+
pending("pending"),
91+
sold("sold");
92+
93+
companion object {
94+
@JvmStatic
95+
@JsonCreator
96+
fun forValue(value: kotlin.String): Status {
97+
return values().first{it -> it.value == value}
98+
}
99+
}
91100
}
92101

93102
}

samples/server/petstore/kotlin-spring-default/src/main/kotlin/org/openapitools/model/AnyOfUserOrPetOrArrayString.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.openapitools.model
22

33
import java.util.Objects
4+
import com.fasterxml.jackson.annotation.JsonCreator
45
import com.fasterxml.jackson.annotation.JsonProperty
56
import com.fasterxml.jackson.annotation.JsonValue
67
import org.openapitools.model.Category
@@ -83,11 +84,19 @@ data class AnyOfUserOrPetOrArrayString(
8384
* pet status in the store
8485
* Values: available,pending,sold
8586
*/
86-
enum class Status(val value: kotlin.String) {
87-
88-
@JsonProperty("available") available("available"),
89-
@JsonProperty("pending") pending("pending"),
90-
@JsonProperty("sold") sold("sold")
87+
enum class Status(@get:JsonValue val value: kotlin.String) {
88+
89+
available("available"),
90+
pending("pending"),
91+
sold("sold");
92+
93+
companion object {
94+
@JvmStatic
95+
@JsonCreator
96+
fun forValue(value: kotlin.String): Status {
97+
return values().first{it -> it.value == value}
98+
}
99+
}
91100
}
92101

93102
}

samples/server/petstore/kotlin-spring-default/src/main/kotlin/org/openapitools/model/Order.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.openapitools.model
22

33
import java.util.Objects
4+
import com.fasterxml.jackson.annotation.JsonCreator
45
import com.fasterxml.jackson.annotation.JsonProperty
56
import com.fasterxml.jackson.annotation.JsonValue
67
import javax.validation.constraints.DecimalMax
@@ -48,11 +49,19 @@ data class Order(
4849
* Order Status
4950
* Values: placed,approved,delivered
5051
*/
51-
enum class Status(val value: kotlin.String) {
52+
enum class Status(@get:JsonValue val value: kotlin.String) {
5253

53-
@JsonProperty("placed") placed("placed"),
54-
@JsonProperty("approved") approved("approved"),
55-
@JsonProperty("delivered") delivered("delivered")
54+
placed("placed"),
55+
approved("approved"),
56+
delivered("delivered");
57+
58+
companion object {
59+
@JvmStatic
60+
@JsonCreator
61+
fun forValue(value: kotlin.String): Status {
62+
return values().first{it -> it.value == value}
63+
}
64+
}
5665
}
5766

5867
}

0 commit comments

Comments
 (0)