Skip to content

Commit

Permalink
Support for "x-enum-descriptions"
Browse files Browse the repository at this point in the history
  • Loading branch information
jmini committed Jan 9, 2019
1 parent 814a697 commit 20697ec
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -338,7 +337,7 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> 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);
}
Expand Down Expand Up @@ -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<String, Object> extensions = var.mostInnerItems != null ? var.mostInnerItems.getVendorExtensions() : var.getVendorExtensions();
updateEnumVarsWithExtensions(enumVars, extensions);
allowableValues.put("enumVars", enumVars);
Expand All @@ -4105,13 +4104,19 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
}
}

private void updateEnumVarsWithExtensions
(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions) {
if (vendorExtensions != null && vendorExtensions.containsKey("x-enum-varnames")) {
List<String> alias = (List<String>) vendorExtensions.get("x-enum-varnames");
int size = Math.min(enumVars.size(), alias.size());
private void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions) {
if (vendorExtensions != null) {
updateEnumVarsWithExtensions(enumVars, vendorExtensions, "x-enum-varnames", "name");
updateEnumVarsWithExtensions(enumVars, vendorExtensions, "x-enum-descriptions", "enumDescription");
}
}

private void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions, String extensionKey, String key) {
if (vendorExtensions.containsKey(extensionKey)) {
List<String> values = (List<String>) 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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} {
{{#allowableValues}}
{{#enumVars}}
{{#enumDescription}}
/**
* {{enumDescription}}
*/
{{/enumDescription}}
{{{name}}}({{{value}}}){{^-last}},
{{/-last}}{{#-last}};{{/-last}}
{{/enumVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -575,7 +577,10 @@ private Map<String, Object> codegenModelWithXEnumVarName() {
cm.setAllowableValues(allowableValues);
cm.dataType = "String";
final List<String> aliases = Arrays.asList("DOGVAR", "CATVAR");
Map<String, Object> extentions = Collections.singletonMap("x-enum-varnames", aliases);
final List<String> descriptions = Arrays.asList("This is a dog", "This is a cat");
Map<String, Object> extentions = new HashMap<>();
extentions.put("x-enum-varnames", aliases);
extentions.put("x-enum-descriptions", descriptions);
cm.setVendorExtensions(extentions);
cm.setVars(Collections.emptyList());
Map<String, Object> objs = Collections.singletonMap("models", Collections.singletonList(Collections.singletonMap("model", cm)));
Expand Down

0 comments on commit 20697ec

Please sign in to comment.