Skip to content

Commit 6dcaa89

Browse files
committed
Improvements on enum var name in Java client
* Place each enum var in a separate line * Truncate common prefix when present
1 parent cbc2fb2 commit 6dcaa89

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,20 +351,34 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
351351
Map<String, Object> allowableValues = var.allowableValues;
352352

353353
// handle ArrayProperty
354-
if(var.items != null) {
354+
if (var.items != null) {
355355
allowableValues = var.items.allowableValues;
356356
}
357357

358-
if (allowableValues == null)
358+
if (allowableValues == null) {
359359
continue;
360+
}
360361
List<String> values = (List<String>) allowableValues.get("values");
361-
// put "enumVars" map into `allowableValues", including `name` and `value`
362-
if (values == null)
362+
if (values == null) {
363363
continue;
364+
}
365+
366+
// put "enumVars" map into `allowableValues", including `name` and `value`
364367
List<Map<String, String>> enumVars = new ArrayList<Map<String, String>>();
368+
String commonPrefix = findCommonPrefixOfVars(values);
369+
int truncateIdx = "".equals(commonPrefix) ? 0 : commonPrefix.length();
365370
for (String value : values) {
366371
Map<String, String> enumVar = new HashMap<String, String>();
367-
enumVar.put("name", toVarName(value.toUpperCase()));
372+
String enumName;
373+
if (truncateIdx == 0) {
374+
enumName = value;
375+
} else {
376+
enumName = value.substring(truncateIdx);
377+
if ("".equals(enumName)) {
378+
enumName = value;
379+
}
380+
}
381+
enumVar.put("name", toEnumVarName(enumName));
368382
enumVar.put("value", value);
369383
enumVars.add(enumVar);
370384
}
@@ -374,6 +388,17 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
374388
return objs;
375389
}
376390

391+
private String findCommonPrefixOfVars(List<String> vars) {
392+
String prefix = StringUtils.getCommonPrefix(vars.toArray(new String[vars.size()]));
393+
// exclude trailing characters that should be part of a valid variable
394+
// e.g. ["status-on", "status-off"] => "status-" (not "status-o")
395+
return prefix.replaceAll("[a-zA-Z0-9]+\\z", "");
396+
}
397+
398+
private String toEnumVarName(String value) {
399+
return value.replaceAll("\\W+", "_").toUpperCase();
400+
}
401+
377402
private CodegenModel reconcileInlineEnums(CodegenModel codegenModel, CodegenModel parentCodegenModel) {
378403
// This generator uses inline classes to define enums, which breaks when
379404
// dealing with models that have subTypes. To clean this up, we will analyze

modules/swagger-codegen/src/main/resources/Java/enumClass.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
public enum {{datatypeWithEnum}} {
2-
{{#allowableValues}}{{#enumVars}}{{name}}("{{value}}"){{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
2+
{{#allowableValues}}{{#enumVars}}{{name}}("{{value}}"){{^-last}},
3+
{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
34

45
private String value;
56

samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Order.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
@ApiModel(description = "")
13-
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T11:35:58.351+08:00")
13+
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-21T17:24:54.566+08:00")
1414
public class Order {
1515

1616
private Long id = null;
@@ -19,7 +19,9 @@ public class Order {
1919
private Date shipDate = null;
2020

2121
public enum StatusEnum {
22-
PLACED("placed"), APPROVED("approved"), DELIVERED("delivered");
22+
PLACED("placed"),
23+
APPROVED("approved"),
24+
DELIVERED("delivered");
2325

2426
private String value;
2527

samples/client/petstore/java/default/src/main/java/io/swagger/client/model/Pet.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
@ApiModel(description = "")
15-
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T11:35:58.351+08:00")
15+
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-21T17:24:54.566+08:00")
1616
public class Pet {
1717

1818
private Long id = null;
@@ -22,7 +22,9 @@ public class Pet {
2222
private List<Tag> tags = new ArrayList<Tag>();
2323

2424
public enum StatusEnum {
25-
AVAILABLE("available"), PENDING("pending"), SOLD("sold");
25+
AVAILABLE("available"),
26+
PENDING("pending"),
27+
SOLD("sold");
2628

2729
private String value;
2830

0 commit comments

Comments
 (0)