Skip to content

Commit

Permalink
Merge pull request #12076 from aethanol/go-fix-enum-numbers
Browse files Browse the repository at this point in the history
Go fix enum numbers
  • Loading branch information
HugoMario authored Aug 6, 2023
2 parents a620537 + 4b34f12 commit de33c6f
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,10 @@ public String toEnumVarName(String name, String datatype) {
if ("int".equals(datatype) || "int32".equals(datatype) || "int64".equals(datatype)
|| "uint".equals(datatype) || "uint32".equals(datatype) || "uint64".equals(datatype)
|| "float".equals(datatype) || "float32".equals(datatype) || "float64".equals(datatype)) {
String varName = name;
String varName = "NUMBER_" + name;
varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
varName = varName.replaceAll("\\.", "_DOT_");
if (varName.matches("\\d.*")) {
return "_" + varName;
}
return varName;
}

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

if (isReservedWord(enumName) || enumName.matches("\\d.*")) { // reserved word or starts with number
// starts with a number
if (enumName.matches("\\d.*")) {
enumName = "_" + enumName;
}

// reserved word
if (isReservedWord(enumName)) {
return escapeReservedWord(enumName);
} else {
return enumName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type {{{classname}}} {{^format}}{{dataType}}{{/format}}{{#format}}{{{format}}}{{
const (
{{#allowableValues}}
{{#enumVars}}
{{name}} {{{classname}}} = {{{value}}}
{{name}}_{{{classname}}} {{{classname}}} = {{{value}}}
{{/enumVars}}
{{/allowableValues}}
){{/isEnum}}{{^isEnum}}{{#description}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.swagger.codegen.go;

import io.swagger.codegen.DefaultCodegen;
import org.testng.Assert;
import org.testng.annotations.Test;

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

@Test(description = "test enum variable names for reserved words")
public void testEnumReservedWord() throws Exception {
final DefaultCodegen codegen = new GoClientCodegen();
Assert.assertEquals(codegen.toEnumVarName("IF", null), "IF_");
Assert.assertEquals(codegen.toEnumVarName("const", null), "CONST_");
Assert.assertEquals(codegen.toEnumVarName("INTERFACE", null), "INTERFACE_");
// should not escape non-reserved
Assert.assertEquals(codegen.toEnumVarName("hello", null), "HELLO");
}

@Test(description = "test enum variable names for numbers")
public void testEnumNumber() throws Exception {
final DefaultCodegen codegen = new GoClientCodegen();
Assert.assertEquals(codegen.toEnumVarName("1", "int32"), "NUMBER_1");
Assert.assertEquals(codegen.toEnumVarName("-1", "int32"), "NUMBER_MINUS_1");
Assert.assertEquals(codegen.toEnumVarName("+1", "int32"), "NUMBER_PLUS_1");
Assert.assertEquals(codegen.toEnumVarName("1.1", "float64"), "NUMBER_1_DOT_1");
Assert.assertEquals(codegen.toEnumVarName("-1.2", "float64"), "NUMBER_MINUS_1_DOT_2");
Assert.assertEquals(codegen.toEnumVarName("+1.2", "float64"), "NUMBER_PLUS_1_DOT_2");
}

@Test(description = "test enum variable names for strings that start with a number")
public void testEnumStringNumber() throws Exception {
final DefaultCodegen codegen = new GoClientCodegen();
Assert.assertEquals(codegen.toEnumVarName("1", null), "_1");
Assert.assertEquals(codegen.toEnumVarName("1_SAMPLE", null), "_1_SAMPLE");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.languages.GoClientCodegen;
import io.swagger.codegen.languages.JavaClientCodegen;
import io.swagger.codegen.languages.PhpClientCodegen;
import io.swagger.models.ArrayModel;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.DateTimeProperty;
import io.swagger.models.properties.LongProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.models.Swagger;
import io.swagger.models.properties.*;

import com.google.common.collect.Sets;
import io.swagger.parser.SwaggerParser;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.HashMap;

@SuppressWarnings("static-method")
public class GoModelTest {

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

@Test(description = "convert a go model with an enum")
public void enumMdoelValueTest() {
final StringProperty enumProperty = new StringProperty();
enumProperty.setEnum(Arrays.asList("VALUE1", "VALUE2", "VALUE3"));
final ModelImpl model = new ModelImpl().property("name", enumProperty);

final DefaultCodegen codegen = new GoClientCodegen();
final CodegenModel cm = codegen.fromModel("sample", model);

Assert.assertEquals(cm.vars.size(), 1);

final CodegenProperty enumVar = cm.vars.get(0);
Assert.assertEquals(enumVar.baseName, "name");
Assert.assertEquals(enumVar.datatype, "string");
Assert.assertEquals(enumVar.datatypeWithEnum, "NAME");
Assert.assertEquals(enumVar.name, "Name");
Assert.assertEquals(enumVar.defaultValue, "null");
Assert.assertEquals(enumVar.baseType, "string");
Assert.assertTrue(enumVar.isEnum);
}


@DataProvider(name = "modelNames")
public static Object[][] primeNumbers() {
return new Object[][] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.20-SNAPSHOT
2.4.31-SNAPSHOT
68 changes: 34 additions & 34 deletions samples/client/petstore/go/go-petstore/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ paths:
schema:
$ref: "#/definitions/Pet"
x-exportParamName: "Body"
responses:
"405":
description: "Invalid input"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
responses:
"405":
description: "Invalid input"
put:
tags:
- "pet"
Expand All @@ -78,17 +78,17 @@ paths:
schema:
$ref: "#/definitions/Pet"
x-exportParamName: "Body"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
responses:
"400":
description: "Invalid ID supplied"
"404":
description: "Pet not found"
"405":
description: "Validation exception"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
/pet/findByStatus:
get:
tags:
Expand All @@ -114,6 +114,10 @@ paths:
default: "available"
collectionFormat: "csv"
x-exportParamName: "Status"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
responses:
"200":
description: "successful operation"
Expand All @@ -123,10 +127,6 @@ paths:
$ref: "#/definitions/Pet"
"400":
description: "Invalid status value"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
/pet/findByTags:
get:
tags:
Expand All @@ -148,6 +148,11 @@ paths:
type: "string"
collectionFormat: "csv"
x-exportParamName: "Tags"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
deprecated: true
responses:
"200":
description: "successful operation"
Expand All @@ -157,11 +162,6 @@ paths:
$ref: "#/definitions/Pet"
"400":
description: "Invalid tag value"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
deprecated: true
/pet/{petId}:
get:
tags:
Expand All @@ -180,6 +180,8 @@ paths:
type: "integer"
format: "int64"
x-exportParamName: "PetId"
security:
- api_key: []
responses:
"200":
description: "successful operation"
Expand All @@ -189,8 +191,6 @@ paths:
description: "Invalid ID supplied"
"404":
description: "Pet not found"
security:
- api_key: []
post:
tags:
- "pet"
Expand Down Expand Up @@ -224,13 +224,13 @@ paths:
type: "string"
x-exportParamName: "Status"
x-optionalDataType: "String"
responses:
"405":
description: "Invalid input"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
responses:
"405":
description: "Invalid input"
delete:
tags:
- "pet"
Expand All @@ -254,13 +254,13 @@ paths:
type: "integer"
format: "int64"
x-exportParamName: "PetId"
responses:
"400":
description: "Invalid pet value"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
responses:
"400":
description: "Invalid pet value"
/pet/{petId}/uploadImage:
post:
tags:
Expand Down Expand Up @@ -293,15 +293,15 @@ paths:
required: false
type: "file"
x-exportParamName: "File"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
responses:
"200":
description: "successful operation"
schema:
$ref: "#/definitions/ApiResponse"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
/store/inventory:
get:
tags:
Expand All @@ -312,6 +312,8 @@ paths:
produces:
- "application/json"
parameters: []
security:
- api_key: []
responses:
"200":
description: "successful operation"
Expand All @@ -320,8 +322,6 @@ paths:
additionalProperties:
type: "integer"
format: "int32"
security:
- api_key: []
/store/order:
post:
tags:
Expand Down Expand Up @@ -613,13 +613,13 @@ paths:
schema:
$ref: "#/definitions/Client"
x-exportParamName: "Body"
security:
- api_key_query: []
responses:
"200":
description: "successful operation"
schema:
$ref: "#/definitions/Client"
security:
- api_key_query: []
/fake:
get:
tags:
Expand Down Expand Up @@ -862,13 +862,13 @@ paths:
type: "string"
x-exportParamName: "Callback"
x-optionalDataType: "String"
security:
- http_basic_test: []
responses:
"400":
description: "Invalid username supplied"
"404":
description: "User not found"
security:
- http_basic_test: []
patch:
tags:
- "fake"
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/go/go-petstore/model_boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ type Boolean bool

// List of Boolean
const (
TRUE Boolean = true
FALSE Boolean = false
TRUE_Boolean Boolean = true
FALSE_Boolean Boolean = false
)
6 changes: 3 additions & 3 deletions samples/client/petstore/go/go-petstore/model_enum_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type EnumClass string

// List of EnumClass
const (
ABC EnumClass = "_abc"
EFG EnumClass = "-efg"
XYZ EnumClass = "(xyz)"
ABC_EnumClass EnumClass = "_abc"
EFG_EnumClass EnumClass = "-efg"
XYZ_EnumClass EnumClass = "(xyz)"
)
14 changes: 7 additions & 7 deletions samples/client/petstore/go/go-petstore/model_ints.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type Ints int32

// List of Ints
const (
_0 Ints = 0
_1 Ints = 1
_2 Ints = 2
_3 Ints = 3
_4 Ints = 4
_5 Ints = 5
_6 Ints = 6
NUMBER_0_Ints Ints = 0
NUMBER_1_Ints Ints = 1
NUMBER_2_Ints Ints = 2
NUMBER_3_Ints Ints = 3
NUMBER_4_Ints Ints = 4
NUMBER_5_Ints Ints = 5
NUMBER_6_Ints Ints = 6
)
Loading

0 comments on commit de33c6f

Please sign in to comment.