Skip to content

Commit 611eb87

Browse files
committed
fix open api definition to side-step issues with local enum schema definition (basically avoid existing bug)
1 parent 6159bca commit 611eb87

8 files changed

Lines changed: 82 additions & 91 deletions

File tree

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/spring/KotlinSpringServerCodegenTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,11 +1107,11 @@ public void generateModelWithRequiredFalseWithDefaults() throws Exception {
11071107
"@get:JsonProperty(\"nonRequiredWithDefaultDouble\", required = false)",
11081108
"override val nonRequiredWithDefaultDouble: kotlin.Double = 15.45",
11091109
"@get:JsonProperty(\"nonRequiredWithDefaultEnum\", required = false)",
1110-
"override val nonRequiredWithDefaultEnum: Dog.NonRequiredWithDefaultEnum = NonRequiredWithDefaultEnum.THIS",
1110+
"override val nonRequiredWithDefaultEnum: SomeEnum = SomeEnum.ENUMVALUE1",
11111111
"@get:JsonProperty(\"nonRequiredWithDefaultEnumList\", required = false)",
1112-
"override val nonRequiredWithDefaultEnumList: kotlin.collections.List<Dog.NonRequiredWithDefaultEnumList> = arrayListOf(NonRequiredWithDefaultEnumList.THESE,NonRequiredWithDefaultEnumList.THOSE)",
1112+
"override val nonRequiredWithDefaultEnumList: kotlin.collections.List<SomeEnum> = arrayListOf(SomeEnum.ENUMVALUE3,SomeEnum.ENUMVALUE1)",
11131113
"@get:JsonProperty(\"nonRequiredWithDefaultEnumSet\", required = false)",
1114-
"override val nonRequiredWithDefaultEnumSet: kotlin.collections.Set<Dog.NonRequiredWithDefaultEnumSet> = setOf(NonRequiredWithDefaultEnumSet.THEM,NonRequiredWithDefaultEnumSet.THOSE)"
1114+
"override val nonRequiredWithDefaultEnumSet: kotlin.collections.Set<SomeEnum> = setOf(SomeEnum.ENUMVALUE3,SomeEnum.ENUMVALUE1)"
11151115
);
11161116

11171117
Path petPath = Paths.get(outputPath + "/src/main/kotlin/org/openapitools/model/Pet.kt");
@@ -1125,9 +1125,9 @@ public void generateModelWithRequiredFalseWithDefaults() throws Exception {
11251125
"val nonRequiredWithDefaultLong: java.math.BigDecimal",
11261126
"val nonRequiredWithDefaultFloat: kotlin.Float",
11271127
"val nonRequiredWithDefaultDouble: kotlin.Double",
1128-
"val nonRequiredWithDefaultEnum: Pet.NonRequiredWithDefaultEnum",
1129-
"val nonRequiredWithDefaultEnumList: Pet.NonRequiredWithDefaultEnumList",
1130-
"val nonRequiredWithDefaultEnumSet: Pet.NonRequiredWithDefaultEnumSet"
1128+
"val nonRequiredWithDefaultEnum: SomeEnum",
1129+
"val nonRequiredWithDefaultEnumList: kotlin.collections.List<SomeEnum>",
1130+
"val nonRequiredWithDefaultEnumSet: kotlin.collections.Set<SomeEnum>"
11311131
);
11321132
}
11331133

modules/openapi-generator/src/test/resources/3_0/kotlin/petstore-with-x-kotlin-implements.yaml

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -554,30 +554,18 @@ components:
554554
format: double
555555
default: 15.45
556556
nonRequiredWithDefaultEnum:
557-
type: string
558-
enum:
559-
- THIS
560-
- THAT
561-
default: THIS
557+
$ref: '#/components/schemas/SomeEnum'
562558
nonRequiredWithDefaultEnumList:
563559
type: array
564560
items:
565-
type: string
566-
enum:
567-
- THESE
568-
- THOSE
569-
- THEM
570-
default: [ THESE, THOSE ]
561+
$ref: '#/components/schemas/SomeEnum'
562+
default: [ ENUMVALUE3, ENUMVALUE1 ]
571563
nonRequiredWithDefaultEnumSet:
572564
type: array
573565
uniqueItems: true
574566
items:
575-
type: string
576-
enum:
577-
- THESE
578-
- THOSE
579-
- THEM
580-
default: [ THEM, THOSE ]
567+
$ref: '#/components/schemas/SomeEnum'
568+
default: [ ENUMVALUE3, ENUMVALUE1 ]
581569
photoUrls:
582570
type: array
583571
items:
@@ -635,6 +623,13 @@ components:
635623
format: int64
636624
name:
637625
type: string
626+
SomeEnum:
627+
type: string
628+
enum:
629+
- ENUMVALUE1
630+
- ENUMVALUE2
631+
- ENUMVALUE3
632+
default: ENUMVALUE1
638633
Color:
639634
x-kotlin-implements: [ com.some.pack.WithDefaultMethods ]
640635
type: string

samples/server/petstore/kotlin-springboot-x-kotlin-implements/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ src/main/kotlin/org/openapitools/model/Dog.kt
1919
src/main/kotlin/org/openapitools/model/ModelApiResponse.kt
2020
src/main/kotlin/org/openapitools/model/Order.kt
2121
src/main/kotlin/org/openapitools/model/Pet.kt
22+
src/main/kotlin/org/openapitools/model/SomeEnum.kt
2223
src/main/kotlin/org/openapitools/model/Tag.kt
2324
src/main/kotlin/org/openapitools/model/User.kt

samples/server/petstore/kotlin-springboot-x-kotlin-implements/src/main/kotlin/org/openapitools/configuration/EnumConverterConfiguration.kt

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

33
import org.openapitools.model.Color
4+
import org.openapitools.model.SomeEnum
45

56
import org.springframework.context.annotation.Bean
67
import org.springframework.context.annotation.Configuration
@@ -22,5 +23,11 @@ class EnumConverterConfiguration {
2223
override fun convert(source: kotlin.String): Color = Color.forValue(source)
2324
}
2425
}
26+
@Bean(name = ["org.openapitools.configuration.EnumConverterConfiguration.someEnumConverter"])
27+
fun someEnumConverter(): Converter<kotlin.String, SomeEnum> {
28+
return object: Converter<kotlin.String, SomeEnum> {
29+
override fun convert(source: kotlin.String): SomeEnum = SomeEnum.forValue(source)
30+
}
31+
}
2532

2633
}

samples/server/petstore/kotlin-springboot-x-kotlin-implements/src/main/kotlin/org/openapitools/model/Cat.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.annotation.JsonValue
88
import org.openapitools.model.Category
99
import org.openapitools.model.Color
1010
import org.openapitools.model.Pet
11+
import org.openapitools.model.SomeEnum
1112
import org.openapitools.model.Tag
1213
import java.io.Serializable
1314
import javax.validation.constraints.DecimalMax
@@ -70,14 +71,17 @@ data class Cat(
7071
@get:JsonProperty("nonRequiredWithDefaultDouble", required = false)
7172
override val nonRequiredWithDefaultDouble: kotlin.Double = 15.45,
7273

74+
@field:Valid
7375
@get:JsonProperty("nonRequiredWithDefaultEnum", required = false)
74-
override val nonRequiredWithDefaultEnum: Cat.NonRequiredWithDefaultEnum = NonRequiredWithDefaultEnum.THIS,
76+
override val nonRequiredWithDefaultEnum: SomeEnum = SomeEnum.ENUMVALUE1,
7577

78+
@field:Valid
7679
@get:JsonProperty("nonRequiredWithDefaultEnumList", required = false)
77-
override val nonRequiredWithDefaultEnumList: kotlin.collections.List<Cat.NonRequiredWithDefaultEnumList> = arrayListOf(NonRequiredWithDefaultEnumList.THESE,NonRequiredWithDefaultEnumList.THOSE),
80+
override val nonRequiredWithDefaultEnumList: kotlin.collections.List<SomeEnum> = arrayListOf(SomeEnum.ENUMVALUE3,SomeEnum.ENUMVALUE1),
7881

82+
@field:Valid
7983
@get:JsonProperty("nonRequiredWithDefaultEnumSet", required = false)
80-
override val nonRequiredWithDefaultEnumSet: kotlin.collections.Set<Cat.NonRequiredWithDefaultEnumSet> = setOf(NonRequiredWithDefaultEnumSet.THEM,NonRequiredWithDefaultEnumSet.THOSE),
84+
override val nonRequiredWithDefaultEnumSet: kotlin.collections.Set<SomeEnum> = setOf(SomeEnum.ENUMVALUE3,SomeEnum.ENUMVALUE1),
8185

8286
@field:Valid
8387
@get:JsonProperty("tags", required = false)

samples/server/petstore/kotlin-springboot-x-kotlin-implements/src/main/kotlin/org/openapitools/model/Dog.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.annotation.JsonValue
88
import org.openapitools.model.Category
99
import org.openapitools.model.Color
1010
import org.openapitools.model.Pet
11+
import org.openapitools.model.SomeEnum
1112
import org.openapitools.model.Tag
1213
import java.io.Serializable
1314
import javax.validation.constraints.DecimalMax
@@ -74,14 +75,17 @@ data class Dog(
7475
@get:JsonProperty("nonRequiredWithDefaultDouble", required = false)
7576
override val nonRequiredWithDefaultDouble: kotlin.Double = 15.45,
7677

78+
@field:Valid
7779
@get:JsonProperty("nonRequiredWithDefaultEnum", required = false)
78-
override val nonRequiredWithDefaultEnum: Dog.NonRequiredWithDefaultEnum = NonRequiredWithDefaultEnum.THIS,
80+
override val nonRequiredWithDefaultEnum: SomeEnum = SomeEnum.ENUMVALUE1,
7981

82+
@field:Valid
8083
@get:JsonProperty("nonRequiredWithDefaultEnumList", required = false)
81-
override val nonRequiredWithDefaultEnumList: kotlin.collections.List<Dog.NonRequiredWithDefaultEnumList> = arrayListOf(NonRequiredWithDefaultEnumList.THESE,NonRequiredWithDefaultEnumList.THOSE),
84+
override val nonRequiredWithDefaultEnumList: kotlin.collections.List<SomeEnum> = arrayListOf(SomeEnum.ENUMVALUE3,SomeEnum.ENUMVALUE1),
8285

86+
@field:Valid
8387
@get:JsonProperty("nonRequiredWithDefaultEnumSet", required = false)
84-
override val nonRequiredWithDefaultEnumSet: kotlin.collections.Set<Dog.NonRequiredWithDefaultEnumSet> = setOf(NonRequiredWithDefaultEnumSet.THEM,NonRequiredWithDefaultEnumSet.THOSE),
88+
override val nonRequiredWithDefaultEnumSet: kotlin.collections.Set<SomeEnum> = setOf(SomeEnum.ENUMVALUE3,SomeEnum.ENUMVALUE1),
8589

8690
@field:Valid
8791
@get:JsonProperty("tags", required = false)

samples/server/petstore/kotlin-springboot-x-kotlin-implements/src/main/kotlin/org/openapitools/model/Pet.kt

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo
1010
import com.fasterxml.jackson.annotation.JsonValue
1111
import org.openapitools.model.Category
1212
import org.openapitools.model.Color
13+
import org.openapitools.model.SomeEnum
1314
import org.openapitools.model.Tag
1415
import java.io.Serializable
1516
import javax.validation.constraints.DecimalMax
@@ -90,13 +91,13 @@ interface Pet : Serializable, com.some.pack.Named, com.some.pack.WithCategory, c
9091
val nonRequiredWithDefaultDouble: kotlin.Double
9192

9293

93-
val nonRequiredWithDefaultEnum: Pet.NonRequiredWithDefaultEnum
94+
val nonRequiredWithDefaultEnum: SomeEnum
9495

9596

96-
val nonRequiredWithDefaultEnumList: Pet.NonRequiredWithDefaultEnumList
97+
val nonRequiredWithDefaultEnumList: kotlin.collections.List<SomeEnum>
9798

9899

99-
val nonRequiredWithDefaultEnumSet: Pet.NonRequiredWithDefaultEnumSet
100+
val nonRequiredWithDefaultEnumSet: kotlin.collections.Set<SomeEnum>
100101

101102

102103
val tags: kotlin.collections.List<Tag>?
@@ -105,65 +106,6 @@ interface Pet : Serializable, com.some.pack.Named, com.some.pack.WithCategory, c
105106
val color: Color?
106107

107108

108-
/**
109-
*
110-
* Values: THIS,THAT
111-
*/
112-
enum class NonRequiredWithDefaultEnum(@get:JsonValue val value: kotlin.String) {
113-
114-
THIS("THIS"),
115-
THAT("THAT");
116-
117-
companion object {
118-
@JvmStatic
119-
@JsonCreator
120-
fun forValue(value: kotlin.String): NonRequiredWithDefaultEnum {
121-
return values().firstOrNull{it -> it.value == value}
122-
?: throw IllegalArgumentException("Unexpected value '$value' for enum 'Pet'")
123-
}
124-
}
125-
}
126-
127-
/**
128-
*
129-
* Values: THESE,THOSE,THEM
130-
*/
131-
enum class NonRequiredWithDefaultEnumList(@get:JsonValue val value: kotlin.String) {
132-
133-
THESE("THESE"),
134-
THOSE("THOSE"),
135-
THEM("THEM");
136-
137-
companion object {
138-
@JvmStatic
139-
@JsonCreator
140-
fun forValue(value: kotlin.String): NonRequiredWithDefaultEnumList {
141-
return values().firstOrNull{it -> it.value == value}
142-
?: throw IllegalArgumentException("Unexpected value '$value' for enum 'Pet'")
143-
}
144-
}
145-
}
146-
147-
/**
148-
*
149-
* Values: THESE,THOSE,THEM
150-
*/
151-
enum class NonRequiredWithDefaultEnumSet(@get:JsonValue val value: kotlin.String) {
152-
153-
THESE("THESE"),
154-
THOSE("THOSE"),
155-
THEM("THEM");
156-
157-
companion object {
158-
@JvmStatic
159-
@JsonCreator
160-
fun forValue(value: kotlin.String): NonRequiredWithDefaultEnumSet {
161-
return values().firstOrNull{it -> it.value == value}
162-
?: throw IllegalArgumentException("Unexpected value '$value' for enum 'Pet'")
163-
}
164-
}
165-
}
166-
167109
companion object {
168110
private const val serialVersionUID: kotlin.Long = 1
169111
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.openapitools.model
2+
3+
import java.util.Locale
4+
import java.util.Objects
5+
import com.fasterxml.jackson.annotation.JsonValue
6+
import com.fasterxml.jackson.annotation.JsonCreator
7+
import com.fasterxml.jackson.annotation.JsonProperty
8+
import java.io.Serializable
9+
import javax.validation.constraints.DecimalMax
10+
import javax.validation.constraints.DecimalMin
11+
import javax.validation.constraints.Email
12+
import javax.validation.constraints.Max
13+
import javax.validation.constraints.Min
14+
import javax.validation.constraints.NotNull
15+
import javax.validation.constraints.Pattern
16+
import javax.validation.constraints.Size
17+
import javax.validation.Valid
18+
19+
/**
20+
*
21+
* Values: ENUMVALUE1,ENUMVALUE2,ENUMVALUE3
22+
*/
23+
enum class SomeEnum(@get:JsonValue val value: kotlin.String) {
24+
25+
ENUMVALUE1("ENUMVALUE1"),
26+
ENUMVALUE2("ENUMVALUE2"),
27+
ENUMVALUE3("ENUMVALUE3");
28+
29+
companion object {
30+
@JvmStatic
31+
@JsonCreator
32+
fun forValue(value: kotlin.String): SomeEnum {
33+
return values().firstOrNull{it -> it.value == value}
34+
?: throw IllegalArgumentException("Unexpected value '$value' for enum 'SomeEnum'")
35+
}
36+
}
37+
}
38+

0 commit comments

Comments
 (0)