Skip to content

Commit de33c6f

Browse files
authored
Merge pull request #12076 from aethanol/go-fix-enum-numbers
Go fix enum numbers
2 parents a620537 + 4b34f12 commit de33c6f

File tree

11 files changed

+122
-66
lines changed

11 files changed

+122
-66
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,10 @@ public String toEnumVarName(String name, String datatype) {
486486
if ("int".equals(datatype) || "int32".equals(datatype) || "int64".equals(datatype)
487487
|| "uint".equals(datatype) || "uint32".equals(datatype) || "uint64".equals(datatype)
488488
|| "float".equals(datatype) || "float32".equals(datatype) || "float64".equals(datatype)) {
489-
String varName = name;
489+
String varName = "NUMBER_" + name;
490490
varName = varName.replaceAll("-", "MINUS_");
491491
varName = varName.replaceAll("\\+", "PLUS_");
492492
varName = varName.replaceAll("\\.", "_DOT_");
493-
if (varName.matches("\\d.*")) {
494-
return "_" + varName;
495-
}
496493
return varName;
497494
}
498495

@@ -506,7 +503,13 @@ public String toEnumVarName(String name, String datatype) {
506503
enumName = enumName.replaceFirst("^_", "");
507504
enumName = enumName.replaceFirst("_$", "");
508505

509-
if (isReservedWord(enumName) || enumName.matches("\\d.*")) { // reserved word or starts with number
506+
// starts with a number
507+
if (enumName.matches("\\d.*")) {
508+
enumName = "_" + enumName;
509+
}
510+
511+
// reserved word
512+
if (isReservedWord(enumName)) {
510513
return escapeReservedWord(enumName);
511514
} else {
512515
return enumName;

modules/swagger-codegen/src/main/resources/go/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type {{{classname}}} {{^format}}{{dataType}}{{/format}}{{#format}}{{{format}}}{{
1111
const (
1212
{{#allowableValues}}
1313
{{#enumVars}}
14-
{{name}} {{{classname}}} = {{{value}}}
14+
{{name}}_{{{classname}}} {{{classname}}} = {{{value}}}
1515
{{/enumVars}}
1616
{{/allowableValues}}
1717
){{/isEnum}}{{^isEnum}}{{#description}}

modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoClientCodegenTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.codegen.go;
22

3+
import io.swagger.codegen.DefaultCodegen;
34
import org.testng.Assert;
45
import org.testng.annotations.Test;
56

@@ -37,4 +38,32 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
3738
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
3839
}
3940

41+
@Test(description = "test enum variable names for reserved words")
42+
public void testEnumReservedWord() throws Exception {
43+
final DefaultCodegen codegen = new GoClientCodegen();
44+
Assert.assertEquals(codegen.toEnumVarName("IF", null), "IF_");
45+
Assert.assertEquals(codegen.toEnumVarName("const", null), "CONST_");
46+
Assert.assertEquals(codegen.toEnumVarName("INTERFACE", null), "INTERFACE_");
47+
// should not escape non-reserved
48+
Assert.assertEquals(codegen.toEnumVarName("hello", null), "HELLO");
49+
}
50+
51+
@Test(description = "test enum variable names for numbers")
52+
public void testEnumNumber() throws Exception {
53+
final DefaultCodegen codegen = new GoClientCodegen();
54+
Assert.assertEquals(codegen.toEnumVarName("1", "int32"), "NUMBER_1");
55+
Assert.assertEquals(codegen.toEnumVarName("-1", "int32"), "NUMBER_MINUS_1");
56+
Assert.assertEquals(codegen.toEnumVarName("+1", "int32"), "NUMBER_PLUS_1");
57+
Assert.assertEquals(codegen.toEnumVarName("1.1", "float64"), "NUMBER_1_DOT_1");
58+
Assert.assertEquals(codegen.toEnumVarName("-1.2", "float64"), "NUMBER_MINUS_1_DOT_2");
59+
Assert.assertEquals(codegen.toEnumVarName("+1.2", "float64"), "NUMBER_PLUS_1_DOT_2");
60+
}
61+
62+
@Test(description = "test enum variable names for strings that start with a number")
63+
public void testEnumStringNumber() throws Exception {
64+
final DefaultCodegen codegen = new GoClientCodegen();
65+
Assert.assertEquals(codegen.toEnumVarName("1", null), "_1");
66+
Assert.assertEquals(codegen.toEnumVarName("1_SAMPLE", null), "_1_SAMPLE");
67+
}
68+
4069
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoModelTest.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
import io.swagger.codegen.CodegenProperty;
55
import io.swagger.codegen.DefaultCodegen;
66
import io.swagger.codegen.languages.GoClientCodegen;
7+
import io.swagger.codegen.languages.JavaClientCodegen;
8+
import io.swagger.codegen.languages.PhpClientCodegen;
79
import io.swagger.models.ArrayModel;
810
import io.swagger.models.Model;
911
import io.swagger.models.ModelImpl;
10-
import io.swagger.models.properties.ArrayProperty;
11-
import io.swagger.models.properties.DateTimeProperty;
12-
import io.swagger.models.properties.LongProperty;
13-
import io.swagger.models.properties.MapProperty;
14-
import io.swagger.models.properties.RefProperty;
15-
import io.swagger.models.properties.StringProperty;
12+
import io.swagger.models.Swagger;
13+
import io.swagger.models.properties.*;
1614

1715
import com.google.common.collect.Sets;
16+
import io.swagger.parser.SwaggerParser;
1817
import org.testng.Assert;
1918
import org.testng.annotations.DataProvider;
2019
import org.testng.annotations.Test;
2120

21+
import java.util.Arrays;
22+
import java.util.HashMap;
23+
2224
@SuppressWarnings("static-method")
2325
public class GoModelTest {
2426

@@ -243,6 +245,28 @@ public void mapModelTest() {
243245
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
244246
}
245247

248+
@Test(description = "convert a go model with an enum")
249+
public void enumMdoelValueTest() {
250+
final StringProperty enumProperty = new StringProperty();
251+
enumProperty.setEnum(Arrays.asList("VALUE1", "VALUE2", "VALUE3"));
252+
final ModelImpl model = new ModelImpl().property("name", enumProperty);
253+
254+
final DefaultCodegen codegen = new GoClientCodegen();
255+
final CodegenModel cm = codegen.fromModel("sample", model);
256+
257+
Assert.assertEquals(cm.vars.size(), 1);
258+
259+
final CodegenProperty enumVar = cm.vars.get(0);
260+
Assert.assertEquals(enumVar.baseName, "name");
261+
Assert.assertEquals(enumVar.datatype, "string");
262+
Assert.assertEquals(enumVar.datatypeWithEnum, "NAME");
263+
Assert.assertEquals(enumVar.name, "Name");
264+
Assert.assertEquals(enumVar.defaultValue, "null");
265+
Assert.assertEquals(enumVar.baseType, "string");
266+
Assert.assertTrue(enumVar.isEnum);
267+
}
268+
269+
246270
@DataProvider(name = "modelNames")
247271
public static Object[][] primeNumbers() {
248272
return new Object[][] {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.20-SNAPSHOT
1+
2.4.31-SNAPSHOT

samples/client/petstore/go/go-petstore/api/swagger.yaml

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ paths:
5151
schema:
5252
$ref: "#/definitions/Pet"
5353
x-exportParamName: "Body"
54-
responses:
55-
"405":
56-
description: "Invalid input"
5754
security:
5855
- petstore_auth:
5956
- "write:pets"
6057
- "read:pets"
58+
responses:
59+
"405":
60+
description: "Invalid input"
6161
put:
6262
tags:
6363
- "pet"
@@ -78,17 +78,17 @@ paths:
7878
schema:
7979
$ref: "#/definitions/Pet"
8080
x-exportParamName: "Body"
81+
security:
82+
- petstore_auth:
83+
- "write:pets"
84+
- "read:pets"
8185
responses:
8286
"400":
8387
description: "Invalid ID supplied"
8488
"404":
8589
description: "Pet not found"
8690
"405":
8791
description: "Validation exception"
88-
security:
89-
- petstore_auth:
90-
- "write:pets"
91-
- "read:pets"
9292
/pet/findByStatus:
9393
get:
9494
tags:
@@ -114,6 +114,10 @@ paths:
114114
default: "available"
115115
collectionFormat: "csv"
116116
x-exportParamName: "Status"
117+
security:
118+
- petstore_auth:
119+
- "write:pets"
120+
- "read:pets"
117121
responses:
118122
"200":
119123
description: "successful operation"
@@ -123,10 +127,6 @@ paths:
123127
$ref: "#/definitions/Pet"
124128
"400":
125129
description: "Invalid status value"
126-
security:
127-
- petstore_auth:
128-
- "write:pets"
129-
- "read:pets"
130130
/pet/findByTags:
131131
get:
132132
tags:
@@ -148,6 +148,11 @@ paths:
148148
type: "string"
149149
collectionFormat: "csv"
150150
x-exportParamName: "Tags"
151+
security:
152+
- petstore_auth:
153+
- "write:pets"
154+
- "read:pets"
155+
deprecated: true
151156
responses:
152157
"200":
153158
description: "successful operation"
@@ -157,11 +162,6 @@ paths:
157162
$ref: "#/definitions/Pet"
158163
"400":
159164
description: "Invalid tag value"
160-
security:
161-
- petstore_auth:
162-
- "write:pets"
163-
- "read:pets"
164-
deprecated: true
165165
/pet/{petId}:
166166
get:
167167
tags:
@@ -180,6 +180,8 @@ paths:
180180
type: "integer"
181181
format: "int64"
182182
x-exportParamName: "PetId"
183+
security:
184+
- api_key: []
183185
responses:
184186
"200":
185187
description: "successful operation"
@@ -189,8 +191,6 @@ paths:
189191
description: "Invalid ID supplied"
190192
"404":
191193
description: "Pet not found"
192-
security:
193-
- api_key: []
194194
post:
195195
tags:
196196
- "pet"
@@ -224,13 +224,13 @@ paths:
224224
type: "string"
225225
x-exportParamName: "Status"
226226
x-optionalDataType: "String"
227-
responses:
228-
"405":
229-
description: "Invalid input"
230227
security:
231228
- petstore_auth:
232229
- "write:pets"
233230
- "read:pets"
231+
responses:
232+
"405":
233+
description: "Invalid input"
234234
delete:
235235
tags:
236236
- "pet"
@@ -254,13 +254,13 @@ paths:
254254
type: "integer"
255255
format: "int64"
256256
x-exportParamName: "PetId"
257-
responses:
258-
"400":
259-
description: "Invalid pet value"
260257
security:
261258
- petstore_auth:
262259
- "write:pets"
263260
- "read:pets"
261+
responses:
262+
"400":
263+
description: "Invalid pet value"
264264
/pet/{petId}/uploadImage:
265265
post:
266266
tags:
@@ -293,15 +293,15 @@ paths:
293293
required: false
294294
type: "file"
295295
x-exportParamName: "File"
296+
security:
297+
- petstore_auth:
298+
- "write:pets"
299+
- "read:pets"
296300
responses:
297301
"200":
298302
description: "successful operation"
299303
schema:
300304
$ref: "#/definitions/ApiResponse"
301-
security:
302-
- petstore_auth:
303-
- "write:pets"
304-
- "read:pets"
305305
/store/inventory:
306306
get:
307307
tags:
@@ -312,6 +312,8 @@ paths:
312312
produces:
313313
- "application/json"
314314
parameters: []
315+
security:
316+
- api_key: []
315317
responses:
316318
"200":
317319
description: "successful operation"
@@ -320,8 +322,6 @@ paths:
320322
additionalProperties:
321323
type: "integer"
322324
format: "int32"
323-
security:
324-
- api_key: []
325325
/store/order:
326326
post:
327327
tags:
@@ -613,13 +613,13 @@ paths:
613613
schema:
614614
$ref: "#/definitions/Client"
615615
x-exportParamName: "Body"
616+
security:
617+
- api_key_query: []
616618
responses:
617619
"200":
618620
description: "successful operation"
619621
schema:
620622
$ref: "#/definitions/Client"
621-
security:
622-
- api_key_query: []
623623
/fake:
624624
get:
625625
tags:
@@ -862,13 +862,13 @@ paths:
862862
type: "string"
863863
x-exportParamName: "Callback"
864864
x-optionalDataType: "String"
865+
security:
866+
- http_basic_test: []
865867
responses:
866868
"400":
867869
description: "Invalid username supplied"
868870
"404":
869871
description: "User not found"
870-
security:
871-
- http_basic_test: []
872872
patch:
873873
tags:
874874
- "fake"

samples/client/petstore/go/go-petstore/model_boolean.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ type Boolean bool
1414

1515
// List of Boolean
1616
const (
17-
TRUE Boolean = true
18-
FALSE Boolean = false
17+
TRUE_Boolean Boolean = true
18+
FALSE_Boolean Boolean = false
1919
)

samples/client/petstore/go/go-petstore/model_enum_class.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type EnumClass string
1414

1515
// List of EnumClass
1616
const (
17-
ABC EnumClass = "_abc"
18-
EFG EnumClass = "-efg"
19-
XYZ EnumClass = "(xyz)"
17+
ABC_EnumClass EnumClass = "_abc"
18+
EFG_EnumClass EnumClass = "-efg"
19+
XYZ_EnumClass EnumClass = "(xyz)"
2020
)

samples/client/petstore/go/go-petstore/model_ints.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ type Ints int32
1414

1515
// List of Ints
1616
const (
17-
_0 Ints = 0
18-
_1 Ints = 1
19-
_2 Ints = 2
20-
_3 Ints = 3
21-
_4 Ints = 4
22-
_5 Ints = 5
23-
_6 Ints = 6
17+
NUMBER_0_Ints Ints = 0
18+
NUMBER_1_Ints Ints = 1
19+
NUMBER_2_Ints Ints = 2
20+
NUMBER_3_Ints Ints = 3
21+
NUMBER_4_Ints Ints = 4
22+
NUMBER_5_Ints Ints = 5
23+
NUMBER_6_Ints Ints = 6
2424
)

0 commit comments

Comments
 (0)