From 23f5124726b80f775d58e10e1361a17d0f1aec3f Mon Sep 17 00:00:00 2001 From: Ethan Anderson Date: Wed, 1 Mar 2023 13:33:07 -0600 Subject: [PATCH 1/2] Fix enum strings starting with numbers, unique enums in model.mustache --- .../codegen/languages/AbstractGoCodegen.java | 13 ++++--- .../src/main/resources/go/model.mustache | 2 +- .../codegen/go/GoClientCodegenTest.java | 29 +++++++++++++++ .../io/swagger/codegen/go/GoModelTest.java | 36 +++++++++++++++---- 4 files changed, 68 insertions(+), 12 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractGoCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractGoCodegen.java index 125a9d6f607..08148058789 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractGoCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractGoCodegen.java @@ -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; } @@ -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; diff --git a/modules/swagger-codegen/src/main/resources/go/model.mustache b/modules/swagger-codegen/src/main/resources/go/model.mustache index b01d7fc21e2..ab3399d8cc2 100644 --- a/modules/swagger-codegen/src/main/resources/go/model.mustache +++ b/modules/swagger-codegen/src/main/resources/go/model.mustache @@ -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}} diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoClientCodegenTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoClientCodegenTest.java index ba0db9fba3d..47242a5b1ce 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoClientCodegenTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoClientCodegenTest.java @@ -1,5 +1,6 @@ package io.swagger.codegen.go; +import io.swagger.codegen.DefaultCodegen; import org.testng.Assert; import org.testng.annotations.Test; @@ -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"); + } + } diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoModelTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoModelTest.java index e631b259445..2b3c097ad35 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoModelTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoModelTest.java @@ -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 { @@ -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[][] { From 649a26346e49fd02c3207e918a93bc74cf3a8c9b Mon Sep 17 00:00:00 2001 From: Ethan Anderson Date: Wed, 1 Mar 2023 13:33:21 -0600 Subject: [PATCH 2/2] Regenerate go petstore with ./bin/go-petstore.sh --- .../go/go-petstore/.swagger-codegen/VERSION | 2 +- .../petstore/go/go-petstore/api/swagger.yaml | 68 +++++++++---------- .../petstore/go/go-petstore/model_boolean.go | 4 +- .../go/go-petstore/model_enum_class.go | 6 +- .../petstore/go/go-petstore/model_ints.go | 14 ++-- .../petstore/go/go-petstore/model_numbers.go | 8 +-- .../go/go-petstore/model_outer_enum.go | 6 +- 7 files changed, 54 insertions(+), 54 deletions(-) diff --git a/samples/client/petstore/go/go-petstore/.swagger-codegen/VERSION b/samples/client/petstore/go/go-petstore/.swagger-codegen/VERSION index 1bdaf4866d9..197ad3e64fe 100644 --- a/samples/client/petstore/go/go-petstore/.swagger-codegen/VERSION +++ b/samples/client/petstore/go/go-petstore/.swagger-codegen/VERSION @@ -1 +1 @@ -2.4.20-SNAPSHOT \ No newline at end of file +2.4.31-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/go/go-petstore/api/swagger.yaml b/samples/client/petstore/go/go-petstore/api/swagger.yaml index 7a6cb59c696..a18e1c94927 100644 --- a/samples/client/petstore/go/go-petstore/api/swagger.yaml +++ b/samples/client/petstore/go/go-petstore/api/swagger.yaml @@ -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" @@ -78,6 +78,10 @@ paths: schema: $ref: "#/definitions/Pet" x-exportParamName: "Body" + security: + - petstore_auth: + - "write:pets" + - "read:pets" responses: "400": description: "Invalid ID supplied" @@ -85,10 +89,6 @@ paths: description: "Pet not found" "405": description: "Validation exception" - security: - - petstore_auth: - - "write:pets" - - "read:pets" /pet/findByStatus: get: tags: @@ -114,6 +114,10 @@ paths: default: "available" collectionFormat: "csv" x-exportParamName: "Status" + security: + - petstore_auth: + - "write:pets" + - "read:pets" responses: "200": description: "successful operation" @@ -123,10 +127,6 @@ paths: $ref: "#/definitions/Pet" "400": description: "Invalid status value" - security: - - petstore_auth: - - "write:pets" - - "read:pets" /pet/findByTags: get: tags: @@ -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" @@ -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: @@ -180,6 +180,8 @@ paths: type: "integer" format: "int64" x-exportParamName: "PetId" + security: + - api_key: [] responses: "200": description: "successful operation" @@ -189,8 +191,6 @@ paths: description: "Invalid ID supplied" "404": description: "Pet not found" - security: - - api_key: [] post: tags: - "pet" @@ -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" @@ -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: @@ -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: @@ -312,6 +312,8 @@ paths: produces: - "application/json" parameters: [] + security: + - api_key: [] responses: "200": description: "successful operation" @@ -320,8 +322,6 @@ paths: additionalProperties: type: "integer" format: "int32" - security: - - api_key: [] /store/order: post: tags: @@ -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: @@ -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" diff --git a/samples/client/petstore/go/go-petstore/model_boolean.go b/samples/client/petstore/go/go-petstore/model_boolean.go index 3361f159495..b0856b7ecfd 100644 --- a/samples/client/petstore/go/go-petstore/model_boolean.go +++ b/samples/client/petstore/go/go-petstore/model_boolean.go @@ -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 ) diff --git a/samples/client/petstore/go/go-petstore/model_enum_class.go b/samples/client/petstore/go/go-petstore/model_enum_class.go index 4950a03e747..ca1f2daae70 100644 --- a/samples/client/petstore/go/go-petstore/model_enum_class.go +++ b/samples/client/petstore/go/go-petstore/model_enum_class.go @@ -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)" ) diff --git a/samples/client/petstore/go/go-petstore/model_ints.go b/samples/client/petstore/go/go-petstore/model_ints.go index 5b84b665bf8..d8c19a6ed61 100644 --- a/samples/client/petstore/go/go-petstore/model_ints.go +++ b/samples/client/petstore/go/go-petstore/model_ints.go @@ -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 ) diff --git a/samples/client/petstore/go/go-petstore/model_numbers.go b/samples/client/petstore/go/go-petstore/model_numbers.go index 4cf7b54cef5..d136eb15b68 100644 --- a/samples/client/petstore/go/go-petstore/model_numbers.go +++ b/samples/client/petstore/go/go-petstore/model_numbers.go @@ -14,8 +14,8 @@ type Numbers float32 // List of Numbers const ( - _7 Numbers = 7 - _8 Numbers = 8 - _9 Numbers = 9 - _10 Numbers = 10 + NUMBER_7_Numbers Numbers = 7 + NUMBER_8_Numbers Numbers = 8 + NUMBER_9_Numbers Numbers = 9 + NUMBER_10_Numbers Numbers = 10 ) diff --git a/samples/client/petstore/go/go-petstore/model_outer_enum.go b/samples/client/petstore/go/go-petstore/model_outer_enum.go index 0e724d17c5c..31ce691a70a 100644 --- a/samples/client/petstore/go/go-petstore/model_outer_enum.go +++ b/samples/client/petstore/go/go-petstore/model_outer_enum.go @@ -14,7 +14,7 @@ type OuterEnum string // List of OuterEnum const ( - PLACED OuterEnum = "placed" - APPROVED OuterEnum = "approved" - DELIVERED OuterEnum = "delivered" + PLACED_OuterEnum OuterEnum = "placed" + APPROVED_OuterEnum OuterEnum = "approved" + DELIVERED_OuterEnum OuterEnum = "delivered" )