forked from loafle/openapi-generator-original
Improvements on enum var name in Java client
* Place each enum var in a separate line * Truncate common prefix when present
This commit is contained in:
@@ -351,20 +351,34 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
Map<String, Object> allowableValues = var.allowableValues;
|
||||
|
||||
// handle ArrayProperty
|
||||
if(var.items != null) {
|
||||
if (var.items != null) {
|
||||
allowableValues = var.items.allowableValues;
|
||||
}
|
||||
|
||||
if (allowableValues == null)
|
||||
if (allowableValues == null) {
|
||||
continue;
|
||||
}
|
||||
List<String> values = (List<String>) allowableValues.get("values");
|
||||
// put "enumVars" map into `allowableValues", including `name` and `value`
|
||||
if (values == null)
|
||||
if (values == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// put "enumVars" map into `allowableValues", including `name` and `value`
|
||||
List<Map<String, String>> enumVars = new ArrayList<Map<String, String>>();
|
||||
String commonPrefix = findCommonPrefixOfVars(values);
|
||||
int truncateIdx = "".equals(commonPrefix) ? 0 : commonPrefix.length();
|
||||
for (String value : values) {
|
||||
Map<String, String> enumVar = new HashMap<String, String>();
|
||||
enumVar.put("name", toVarName(value.toUpperCase()));
|
||||
String enumName;
|
||||
if (truncateIdx == 0) {
|
||||
enumName = value;
|
||||
} else {
|
||||
enumName = value.substring(truncateIdx);
|
||||
if ("".equals(enumName)) {
|
||||
enumName = value;
|
||||
}
|
||||
}
|
||||
enumVar.put("name", toEnumVarName(enumName));
|
||||
enumVar.put("value", value);
|
||||
enumVars.add(enumVar);
|
||||
}
|
||||
@@ -374,6 +388,17 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
return objs;
|
||||
}
|
||||
|
||||
private String findCommonPrefixOfVars(List<String> vars) {
|
||||
String prefix = StringUtils.getCommonPrefix(vars.toArray(new String[vars.size()]));
|
||||
// exclude trailing characters that should be part of a valid variable
|
||||
// e.g. ["status-on", "status-off"] => "status-" (not "status-o")
|
||||
return prefix.replaceAll("[a-zA-Z0-9]+\\z", "");
|
||||
}
|
||||
|
||||
private String toEnumVarName(String value) {
|
||||
return value.replaceAll("\\W+", "_").toUpperCase();
|
||||
}
|
||||
|
||||
private CodegenModel reconcileInlineEnums(CodegenModel codegenModel, CodegenModel parentCodegenModel) {
|
||||
// This generator uses inline classes to define enums, which breaks when
|
||||
// dealing with models that have subTypes. To clean this up, we will analyze
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
public enum {{datatypeWithEnum}} {
|
||||
{{#allowableValues}}{{#enumVars}}{{name}}("{{value}}"){{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
|
||||
{{#allowableValues}}{{#enumVars}}{{name}}("{{value}}"){{^-last}},
|
||||
{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
|
||||
|
||||
private String value;
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
|
||||
@ApiModel(description = "")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T11:35:58.351+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-21T17:24:54.566+08:00")
|
||||
public class Order {
|
||||
|
||||
private Long id = null;
|
||||
@@ -19,7 +19,9 @@ public class Order {
|
||||
private Date shipDate = null;
|
||||
|
||||
public enum StatusEnum {
|
||||
PLACED("placed"), APPROVED("approved"), DELIVERED("delivered");
|
||||
PLACED("placed"),
|
||||
APPROVED("approved"),
|
||||
DELIVERED("delivered");
|
||||
|
||||
private String value;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
|
||||
@ApiModel(description = "")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T11:35:58.351+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-21T17:24:54.566+08:00")
|
||||
public class Pet {
|
||||
|
||||
private Long id = null;
|
||||
@@ -22,7 +22,9 @@ public class Pet {
|
||||
private List<Tag> tags = new ArrayList<Tag>();
|
||||
|
||||
public enum StatusEnum {
|
||||
AVAILABLE("available"), PENDING("pending"), SOLD("sold");
|
||||
AVAILABLE("available"),
|
||||
PENDING("pending"),
|
||||
SOLD("sold");
|
||||
|
||||
private String value;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user