diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 71cf4d8c1cc5..a3e844df37b9 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -71,7 +71,6 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; -import java.util.ListIterator; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; @@ -83,8 +82,8 @@ import java.util.stream.Stream; import static org.openapitools.codegen.utils.StringUtils.camelize; -import static org.openapitools.codegen.utils.StringUtils.underscore; import static org.openapitools.codegen.utils.StringUtils.escape; +import static org.openapitools.codegen.utils.StringUtils.underscore; public class DefaultCodegen implements CodegenConfig { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class); @@ -338,7 +337,7 @@ public Map postProcessModelsEnum(Map objs) { enumVar.put("isString", isDataTypeString(cm.dataType)); enumVars.add(enumVar); } - // if "x-enum-varnames" defined, update varnames + // if "x-enum-varnames" or "x-enum-descriptions" defined, update varnames updateEnumVarsWithExtensions(enumVars, cm.getVendorExtensions()); cm.allowableValues.put("enumVars", enumVars); } @@ -4085,7 +4084,7 @@ public void updateCodegenPropertyEnum(CodegenProperty var) { enumVar.put("isString", isDataTypeString(dataType)); enumVars.add(enumVar); } - // if "x-enum-varnames" defined, update varnames + // if "x-enum-varnames" or "x-enum-descriptions" defined, update varnames Map extensions = var.mostInnerItems != null ? var.mostInnerItems.getVendorExtensions() : var.getVendorExtensions(); updateEnumVarsWithExtensions(enumVars, extensions); allowableValues.put("enumVars", enumVars); @@ -4105,13 +4104,19 @@ public void updateCodegenPropertyEnum(CodegenProperty var) { } } - private void updateEnumVarsWithExtensions - (List> enumVars, Map vendorExtensions) { - if (vendorExtensions != null && vendorExtensions.containsKey("x-enum-varnames")) { - List alias = (List) vendorExtensions.get("x-enum-varnames"); - int size = Math.min(enumVars.size(), alias.size()); + private void updateEnumVarsWithExtensions(List> enumVars, Map vendorExtensions) { + if (vendorExtensions != null) { + updateEnumVarsWithExtensions(enumVars, vendorExtensions, "x-enum-varnames", "name"); + updateEnumVarsWithExtensions(enumVars, vendorExtensions, "x-enum-descriptions", "enumDescription"); + } + } + + private void updateEnumVarsWithExtensions(List> enumVars, Map vendorExtensions, String extensionKey, String key) { + if (vendorExtensions.containsKey(extensionKey)) { + List values = (List) vendorExtensions.get(extensionKey); + int size = Math.min(enumVars.size(), values.size()); for (int i = 0; i < size; i++) { - enumVars.get(i).put("name", alias.get(i)); + enumVars.get(i).put(key, values.get(i)); } } } diff --git a/modules/openapi-generator/src/main/resources/Java/modelEnum.mustache b/modules/openapi-generator/src/main/resources/Java/modelEnum.mustache index 6dd5334b771c..3d21e9b27648 100644 --- a/modules/openapi-generator/src/main/resources/Java/modelEnum.mustache +++ b/modules/openapi-generator/src/main/resources/Java/modelEnum.mustache @@ -18,6 +18,11 @@ import com.google.gson.stream.JsonWriter; {{/gson}} public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} { {{#allowableValues}}{{#enumVars}} + {{#enumDescription}} + /** + * {{enumDescription}} + */ + {{/enumDescription}} {{{name}}}({{{value}}}){{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}} diff --git a/modules/openapi-generator/src/main/resources/Java/modelInnerEnum.mustache b/modules/openapi-generator/src/main/resources/Java/modelInnerEnum.mustache index aef0f27b89a8..71dd9a4df166 100644 --- a/modules/openapi-generator/src/main/resources/Java/modelInnerEnum.mustache +++ b/modules/openapi-generator/src/main/resources/Java/modelInnerEnum.mustache @@ -7,6 +7,11 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} { {{#allowableValues}} {{#enumVars}} + {{#enumDescription}} + /** + * {{enumDescription}} + */ + {{/enumDescription}} {{{name}}}({{{value}}}){{^-last}}, {{/-last}}{{#-last}};{{/-last}} {{/enumVars}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index 8463bb220f85..dce52f3b86c5 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -353,9 +353,11 @@ public void postProcessModelsEnumWithExtention() { Assert.assertNotNull(enumVars.get(0)); Assert.assertEquals(enumVars.get(0).getOrDefault("name", ""), "DOGVAR"); Assert.assertEquals(enumVars.get(0).getOrDefault("value", ""), "\"dog\""); + Assert.assertEquals(enumVars.get(0).getOrDefault("enumDescription", ""), "This is a dog"); Assert.assertNotNull(enumVars.get(1)); Assert.assertEquals(enumVars.get(1).getOrDefault("name", ""), "CATVAR"); Assert.assertEquals(enumVars.get(1).getOrDefault("value", ""), "\"cat\""); + Assert.assertEquals(enumVars.get(1).getOrDefault("enumDescription", ""), "This is a cat"); } @Test @@ -575,7 +577,10 @@ private Map codegenModelWithXEnumVarName() { cm.setAllowableValues(allowableValues); cm.dataType = "String"; final List aliases = Arrays.asList("DOGVAR", "CATVAR"); - Map extentions = Collections.singletonMap("x-enum-varnames", aliases); + final List descriptions = Arrays.asList("This is a dog", "This is a cat"); + Map extentions = new HashMap<>(); + extentions.put("x-enum-varnames", aliases); + extentions.put("x-enum-descriptions", descriptions); cm.setVendorExtensions(extentions); cm.setVars(Collections.emptyList()); Map objs = Collections.singletonMap("models", Collections.singletonList(Collections.singletonMap("model", cm)));