Skip to content

Commit

Permalink
[Kotlin][Spring] fix #19244 integer enum (#19248)
Browse files Browse the repository at this point in the history
* [Kotlin][Spring] fix #19244 integer enum

* fix embedded array enum
  • Loading branch information
pstorch authored Jul 26, 2024
1 parent c93ec54 commit 37afe57
Show file tree
Hide file tree
Showing 48 changed files with 912 additions and 132 deletions.
13 changes: 13 additions & 0 deletions bin/configs/kotlin-spring-boot-integer-enum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generatorName: kotlin-spring
outputDir: samples/server/petstore/kotlin-springboot-integer-enum
library: spring-boot
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue19244_integer_enum.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-spring
additionalProperties:
interfaceOnly: "true"
skipDefaultInterface: "true"
useTags: "true"
useSpringBoot3: "true"
annotationLibrary: none
documentationProvider: none
enumPropertyNaming: UPPERCASE
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
model.imports.add("JsonProperty");
if (Boolean.TRUE.equals(model.hasEnums)) {
model.imports.add("JsonValue");
model.imports.add("JsonCreator");
}
} else {
//Needed imports for Jackson's JsonCreator
Expand All @@ -840,10 +841,14 @@ public ModelsMap postProcessModelsEnum(ModelsMap objs) {
.filter(cm -> Boolean.TRUE.equals(cm.isEnum) && cm.allowableValues != null)
.forEach(cm -> {
cm.imports.add(importMapping.get("JsonValue"));
cm.imports.add(importMapping.get("JsonCreator"));
cm.imports.add(importMapping.get("JsonProperty"));
Map<String, String> itemJsonValue = new HashMap<>();
itemJsonValue.put("import", importMapping.get("JsonValue"));
imports.add(itemJsonValue);
Map<String, String> itemJsonCreator = new HashMap<>();
itemJsonCreator.put("import", importMapping.get("JsonCreator"));
imports.add(itemJsonCreator);
Map<String, String> itemJsonProperty = new HashMap<>();
itemJsonProperty.put("import", importMapping.get("JsonProperty"));
imports.add(itemJsonProperty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
enum class {{{nameInPascalCase}}}(val value: {{#isContainer}}{{#items}}{{{dataType}}}{{/items}}{{/isContainer}}{{^isContainer}}{{{dataType}}}{{/isContainer}}) {
enum class {{{nameInPascalCase}}}(@get:JsonValue val value: {{#isContainer}}{{#items}}{{{dataType}}}{{/items}}{{/isContainer}}{{^isContainer}}{{{dataType}}}{{/isContainer}}) {
{{#allowableValues}}{{#enumVars}}
@JsonProperty({{{value}}}) {{{name}}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
{{{name}}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}};

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: {{#isContainer}}{{#items}}{{{dataType}}}{{/items}}{{/isContainer}}{{^isContainer}}{{{dataType}}}{{/isContainer}}): {{{nameInPascalCase}}} {
return values().first{it -> it.value == value}
}
}
}
{{/isEnum}}{{/vars}}{{/hasEnums}}
{{#serializableModel}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
enum class {{classname}}(val value: {{dataType}}) {
enum class {{classname}}(@get:JsonValue val value: {{dataType}}) {
{{#allowableValues}}{{#enumVars}}
@JsonProperty({{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}};

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: {{dataType}}): {{classname}} {
return values().first{it -> it.value == value}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
openapi: 3.0.3
info:
description: >-
Example created
version: 1.0.0
title: OpenAPI Stuff API created to reproduce issue
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
paths:
/healthcheck:
get:
summary: Health check endpoint.
operationId: healthcheck
responses:
204:
description: Successful health check
default:
description: Unexpected error
content:
'application/json':
schema:
$ref: '#/components/schemas/ApiError'

components:
schemas:
ApiError:
required:
- errorCode
type: object
properties:
errorCode:
type: integer
enum:
- 0
- 100
x-enum-varnames:
- OK
- ERROR
reasonCode:
$ref: '#/components/schemas/ReasonCode'
ReasonCode:
type: integer
enum:
- 10
- 20
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.model

import java.util.Objects
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import java.io.Serializable
Expand Down Expand Up @@ -42,11 +43,19 @@ data class Order(
* Order Status
* Values: placed,approved,delivered
*/
enum class Status(val value: kotlin.String) {
enum class Status(@get:JsonValue val value: kotlin.String) {

@JsonProperty("placed") placed("placed"),
@JsonProperty("approved") approved("approved"),
@JsonProperty("delivered") delivered("delivered")
placed("placed"),
approved("approved"),
delivered("delivered");

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: kotlin.String): Status {
return values().first{it -> it.value == value}
}
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.model

import java.util.Objects
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import org.openapitools.model.Category
Expand Down Expand Up @@ -47,11 +48,19 @@ data class Pet(
* pet status in the store
* Values: available,pending,sold
*/
enum class Status(val value: kotlin.String) {
enum class Status(@get:JsonValue val value: kotlin.String) {

@JsonProperty("available") available("available"),
@JsonProperty("pending") pending("pending"),
@JsonProperty("sold") sold("sold")
available("available"),
pending("pending"),
sold("sold");

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: kotlin.String): Status {
return values().first{it -> it.value == value}
}
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.model

import java.util.Objects
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import org.openapitools.model.Category
Expand Down Expand Up @@ -83,11 +84,19 @@ data class AnyOfUserOrPet(
* pet status in the store
* Values: available,pending,sold
*/
enum class Status(val value: kotlin.String) {

@JsonProperty("available") available("available"),
@JsonProperty("pending") pending("pending"),
@JsonProperty("sold") sold("sold")
enum class Status(@get:JsonValue val value: kotlin.String) {

available("available"),
pending("pending"),
sold("sold");

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: kotlin.String): Status {
return values().first{it -> it.value == value}
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.model

import java.util.Objects
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import org.openapitools.model.Category
Expand Down Expand Up @@ -83,11 +84,19 @@ data class AnyOfUserOrPetOrArrayString(
* pet status in the store
* Values: available,pending,sold
*/
enum class Status(val value: kotlin.String) {

@JsonProperty("available") available("available"),
@JsonProperty("pending") pending("pending"),
@JsonProperty("sold") sold("sold")
enum class Status(@get:JsonValue val value: kotlin.String) {

available("available"),
pending("pending"),
sold("sold");

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: kotlin.String): Status {
return values().first{it -> it.value == value}
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.model

import java.util.Objects
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import javax.validation.constraints.DecimalMax
Expand Down Expand Up @@ -48,11 +49,19 @@ data class Order(
* Order Status
* Values: placed,approved,delivered
*/
enum class Status(val value: kotlin.String) {
enum class Status(@get:JsonValue val value: kotlin.String) {

@JsonProperty("placed") placed("placed"),
@JsonProperty("approved") approved("approved"),
@JsonProperty("delivered") delivered("delivered")
placed("placed"),
approved("approved"),
delivered("delivered");

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: kotlin.String): Status {
return values().first{it -> it.value == value}
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.model

import java.util.Objects
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import org.openapitools.model.Category
Expand Down Expand Up @@ -53,11 +54,19 @@ data class Pet(
* pet status in the store
* Values: available,pending,sold
*/
enum class Status(val value: kotlin.String) {
enum class Status(@get:JsonValue val value: kotlin.String) {

@JsonProperty("available") available("available"),
@JsonProperty("pending") pending("pending"),
@JsonProperty("sold") sold("sold")
available("available"),
pending("pending"),
sold("sold");

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: kotlin.String): Status {
return values().first{it -> it.value == value}
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.model

import java.util.Objects
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import org.openapitools.model.Category
Expand Down Expand Up @@ -83,11 +84,19 @@ data class UserOrPet(
* pet status in the store
* Values: available,pending,sold
*/
enum class Status(val value: kotlin.String) {

@JsonProperty("available") available("available"),
@JsonProperty("pending") pending("pending"),
@JsonProperty("sold") sold("sold")
enum class Status(@get:JsonValue val value: kotlin.String) {

available("available"),
pending("pending"),
sold("sold");

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: kotlin.String): Status {
return values().first{it -> it.value == value}
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.model

import java.util.Objects
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import org.openapitools.model.Category
Expand Down Expand Up @@ -83,11 +84,19 @@ data class UserOrPetOrArrayString(
* pet status in the store
* Values: available,pending,sold
*/
enum class Status(val value: kotlin.String) {

@JsonProperty("available") available("available"),
@JsonProperty("pending") pending("pending"),
@JsonProperty("sold") sold("sold")
enum class Status(@get:JsonValue val value: kotlin.String) {

available("available"),
pending("pending"),
sold("sold");

companion object {
@JvmStatic
@JsonCreator
fun forValue(value: kotlin.String): Status {
return values().first{it -> it.value == value}
}
}
}

}
Expand Down
Loading

0 comments on commit 37afe57

Please sign in to comment.