Skip to content

Commit 5b4eb96

Browse files
authored
Merge pull request #3519 from wing328/enum_name_mapping
[Java] better enum naming for symbol
2 parents d5bf43b + 670f103 commit 5b4eb96

File tree

7 files changed

+70
-49
lines changed

7 files changed

+70
-49
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public class DefaultCodegen {
106106
// How to encode special characters like $
107107
// They are translated to words like "Dollar" and prefixed with '
108108
// Then translated back during JSON encoding and decoding
109-
protected Map<Character, String> specialCharReplacements = new HashMap<Character, String>();
109+
protected Map<String, String> specialCharReplacements = new HashMap<String, String>();
110110

111111
public List<CliOption> cliOptions() {
112112
return cliOptions;
@@ -789,21 +789,35 @@ public DefaultCodegen() {
789789
*/
790790
protected void initalizeSpecialCharacterMapping() {
791791
// Initialize special characters
792-
specialCharReplacements.put('$', "Dollar");
793-
specialCharReplacements.put('^', "Caret");
794-
specialCharReplacements.put('|', "Pipe");
795-
specialCharReplacements.put('=', "Equal");
796-
specialCharReplacements.put('*', "Star");
797-
specialCharReplacements.put('-', "Minus");
798-
specialCharReplacements.put('&', "Ampersand");
799-
specialCharReplacements.put('%', "Percent");
800-
specialCharReplacements.put('#', "Hash");
801-
specialCharReplacements.put('@', "At");
802-
specialCharReplacements.put('!', "Exclamation");
803-
specialCharReplacements.put('+', "Plus");
804-
specialCharReplacements.put(':', "Colon");
805-
specialCharReplacements.put('>', "GreaterThan");
806-
specialCharReplacements.put('<', "LessThan");
792+
specialCharReplacements.put("$", "Dollar");
793+
specialCharReplacements.put("^", "Caret");
794+
specialCharReplacements.put("|", "Pipe");
795+
specialCharReplacements.put("=", "Equal");
796+
specialCharReplacements.put("*", "Star");
797+
specialCharReplacements.put("-", "Minus");
798+
specialCharReplacements.put("&", "Ampersand");
799+
specialCharReplacements.put("%", "Percent");
800+
specialCharReplacements.put("#", "Hash");
801+
specialCharReplacements.put("@", "At");
802+
specialCharReplacements.put("!", "Exclamation");
803+
specialCharReplacements.put("+", "Plus");
804+
specialCharReplacements.put(":", "Colon");
805+
specialCharReplacements.put(">", "Greater_Than");
806+
specialCharReplacements.put("<", "Less_Than");
807+
808+
specialCharReplacements.put("<=", "Less_Than_Or_Equal_To");
809+
specialCharReplacements.put(">=", "Greater_Than_Or_Equal_To");
810+
specialCharReplacements.put("!=", "Greater_Than_Or_Equal_To");
811+
}
812+
813+
/**
814+
* Return the symbol name of a symbol
815+
*
816+
* @param input Symbol (e.g. $)
817+
* @return Symbol name (e.g. Dollar)
818+
*/
819+
protected String getSymbolName(String input) {
820+
return specialCharReplacements.get(input);
807821
}
808822

809823
/**

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,11 @@ public String toEnumName(CodegenProperty property) {
729729

730730
@Override
731731
public String toEnumVarName(String value, String datatype) {
732+
// for symbol, e.g. $, #
733+
if (getSymbolName(value) != null) {
734+
return getSymbolName(value).toUpperCase();
735+
}
736+
732737
// number
733738
if ("Integer".equals(datatype) || "Long".equals(datatype) ||
734739
"Float".equals(datatype) || "Double".equals(datatype)) {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ public String getHelp() {
4949
public HaskellServantCodegen() {
5050
super();
5151

52-
// override the mapping for "-" (Minus) to keep the original mapping in Haskell
53-
specialCharReplacements.put('-', "Dash");
52+
// override the mapping to keep the original mapping in Haskell
53+
specialCharReplacements.put("-", "Dash");
54+
specialCharReplacements.put(">", "GreaterThan");
55+
specialCharReplacements.put("<", "LessThan");
5456

5557
// set the output folder here
5658
outputFolder = "generated-code/haskell-servant";
@@ -203,9 +205,9 @@ public void preprocessSwagger(Swagger swagger) {
203205
List<Map<String, Object>> replacements = new ArrayList<>();
204206
Object[] replacementChars = specialCharReplacements.keySet().toArray();
205207
for(int i = 0; i < replacementChars.length; i++) {
206-
Character c = (Character) replacementChars[i];
208+
String c = (String) replacementChars[i];
207209
Map<String, Object> o = new HashMap<>();
208-
o.put("char", Character.toString(c));
210+
o.put("char", c);
209211
o.put("replacement", "'" + specialCharReplacements.get(c));
210212
o.put("hasMore", i != replacementChars.length - 1);
211213
replacements.add(o);

modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,11 +1146,11 @@ definitions:
11461146
EnumArrays:
11471147
type: object
11481148
properties:
1149-
just_enum:
1149+
just_symbol:
11501150
type: string
11511151
enum:
1152-
- bird
1153-
- eagle
1152+
- ">="
1153+
- "$"
11541154
array_enum:
11551155
type: array
11561156
items:

samples/client/petstore/java/okhttp-gson/docs/EnumArrays.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
## Properties
55
Name | Type | Description | Notes
66
------------ | ------------- | ------------- | -------------
7-
**justEnum** | [**JustEnumEnum**](#JustEnumEnum) | | [optional]
7+
**justSymbol** | [**JustSymbolEnum**](#JustSymbolEnum) | | [optional]
88
**arrayEnum** | [**List&lt;ArrayEnumEnum&gt;**](#List&lt;ArrayEnumEnum&gt;) | | [optional]
99

1010

11-
<a name="JustEnumEnum"></a>
12-
## Enum: JustEnumEnum
11+
<a name="JustSymbolEnum"></a>
12+
## Enum: JustSymbolEnum
1313
Name | Value
1414
---- | -----
15-
BIRD | &quot;bird&quot;
16-
EAGLE | &quot;eagle&quot;
15+
GREATER_THAN_OR_EQUAL_TO | &quot;&gt;&#x3D;&quot;
16+
DOLLAR | &quot;$&quot;
1717

1818

1919
<a name="List<ArrayEnumEnum>"></a>

samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/model/EnumArrays.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@
3939

4040
public class EnumArrays {
4141
/**
42-
* Gets or Sets justEnum
42+
* Gets or Sets justSymbol
4343
*/
44-
public enum JustEnumEnum {
45-
@SerializedName("bird")
46-
BIRD("bird"),
44+
public enum JustSymbolEnum {
45+
@SerializedName(">=")
46+
GREATER_THAN_OR_EQUAL_TO(">="),
4747

48-
@SerializedName("eagle")
49-
EAGLE("eagle");
48+
@SerializedName("$")
49+
DOLLAR("$");
5050

5151
private String value;
5252

53-
JustEnumEnum(String value) {
53+
JustSymbolEnum(String value) {
5454
this.value = value;
5555
}
5656

@@ -60,8 +60,8 @@ public String toString() {
6060
}
6161
}
6262

63-
@SerializedName("just_enum")
64-
private JustEnumEnum justEnum = null;
63+
@SerializedName("just_symbol")
64+
private JustSymbolEnum justSymbol = null;
6565

6666
/**
6767
* Gets or Sets arrayEnum
@@ -88,22 +88,22 @@ public String toString() {
8888
@SerializedName("array_enum")
8989
private List<ArrayEnumEnum> arrayEnum = new ArrayList<ArrayEnumEnum>();
9090

91-
public EnumArrays justEnum(JustEnumEnum justEnum) {
92-
this.justEnum = justEnum;
91+
public EnumArrays justSymbol(JustSymbolEnum justSymbol) {
92+
this.justSymbol = justSymbol;
9393
return this;
9494
}
9595

9696
/**
97-
* Get justEnum
98-
* @return justEnum
97+
* Get justSymbol
98+
* @return justSymbol
9999
**/
100100
@ApiModelProperty(example = "null", value = "")
101-
public JustEnumEnum getJustEnum() {
102-
return justEnum;
101+
public JustSymbolEnum getJustSymbol() {
102+
return justSymbol;
103103
}
104104

105-
public void setJustEnum(JustEnumEnum justEnum) {
106-
this.justEnum = justEnum;
105+
public void setJustSymbol(JustSymbolEnum justSymbol) {
106+
this.justSymbol = justSymbol;
107107
}
108108

109109
public EnumArrays arrayEnum(List<ArrayEnumEnum> arrayEnum) {
@@ -139,21 +139,21 @@ public boolean equals(java.lang.Object o) {
139139
return false;
140140
}
141141
EnumArrays enumArrays = (EnumArrays) o;
142-
return Objects.equals(this.justEnum, enumArrays.justEnum) &&
142+
return Objects.equals(this.justSymbol, enumArrays.justSymbol) &&
143143
Objects.equals(this.arrayEnum, enumArrays.arrayEnum);
144144
}
145145

146146
@Override
147147
public int hashCode() {
148-
return Objects.hash(justEnum, arrayEnum);
148+
return Objects.hash(justSymbol, arrayEnum);
149149
}
150150

151151
@Override
152152
public String toString() {
153153
StringBuilder sb = new StringBuilder();
154154
sb.append("class EnumArrays {\n");
155155

156-
sb.append(" justEnum: ").append(toIndentedString(justEnum)).append("\n");
156+
sb.append(" justSymbol: ").append(toIndentedString(justSymbol)).append("\n");
157157
sb.append(" arrayEnum: ").append(toIndentedString(arrayEnum)).append("\n");
158158
sb.append("}");
159159
return sb.toString();

samples/server/petstore/haskell-servant/lib/SwaggerPetstore/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ removeFieldLabelPrefix forParsing prefix =
112112
}
113113
where
114114
replaceSpecialChars field = foldl (&) field (map mkCharReplacement specialChars)
115-
specialChars = [("#", "'Hash"), ("!", "'Exclamation"), ("&", "'Ampersand"), ("@", "'At"), ("$", "'Dollar"), ("%", "'Percent"), ("*", "'Star"), ("+", "'Plus"), ("-", "'Dash"), (":", "'Colon"), ("^", "'Caret"), ("|", "'Pipe"), (">", "'GreaterThan"), ("=", "'Equal"), ("<", "'LessThan")]
115+
specialChars = [("#", "'Hash"), ("!", "'Exclamation"), ("&", "'Ampersand"), ("@", "'At"), ("$", "'Dollar"), ("%", "'Percent"), ("*", "'Star"), ("+", "'Plus"), (">=", "'Greater_Than_Or_Equal_To"), ("-", "'Dash"), ("<=", "'Less_Than_Or_Equal_To"), ("!=", "'Greater_Than_Or_Equal_To"), (":", "'Colon"), ("^", "'Caret"), ("|", "'Pipe"), (">", "'GreaterThan"), ("=", "'Equal"), ("<", "'LessThan")]
116116
mkCharReplacement (replaceStr, searchStr) = T.unpack . replacer (T.pack searchStr) (T.pack replaceStr) . T.pack
117117
replacer = if forParsing then flip T.replace else T.replace
118118

0 commit comments

Comments
 (0)