[Kotlin] Fix (de)serialization of enum classes (#7917)

While inner enum classes from dataClass.mustache work fine, standalone enum classes lack `@JsonProperty` annotations so that Jackson uses names instead of values.
This commit is contained in:
Christopher Schramm
2021-04-16 06:01:05 +02:00
committed by GitHub
parent af992e4b29
commit 4a63aae746
2 changed files with 3 additions and 1 deletions

View File

@@ -540,8 +540,10 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
.filter(cm -> Boolean.TRUE.equals(cm.isEnum) && cm.allowableValues != null)
.forEach(cm -> {
cm.imports.add(importMapping.get("JsonValue"));
cm.imports.add(importMapping.get("JsonProperty"));
Map<String, String> item = new HashMap<>();
item.put("import", importMapping.get("JsonValue"));
item.put("import", importMapping.get("JsonProperty"));
imports.add(item);
});

View File

@@ -4,6 +4,6 @@
*/
enum class {{classname}}(val value: {{dataType}}) {
{{#allowableValues}}{{#enumVars}}
{{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
@JsonProperty({{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}{{/allowableValues}}
}