From d4ed68dc97a75defe76c52f6a33a580888566729 Mon Sep 17 00:00:00 2001 From: Mike Cobbett <77053+techcobweb@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:33:37 +0100 Subject: [PATCH] Openapi2beans needs to handle kebab-case variables in the yaml Signed-off-by: Mike Cobbett <77053+techcobweb@users.noreply.github.com> --- openapi2beans/pkg/generator/JavaStructures.go | 40 ++++++++++-------- openapi2beans/pkg/generator/yaml2java_test.go | 42 ++++++++++++++++++- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/openapi2beans/pkg/generator/JavaStructures.go b/openapi2beans/pkg/generator/JavaStructures.go index e564d6a..795954f 100644 --- a/openapi2beans/pkg/generator/JavaStructures.go +++ b/openapi2beans/pkg/generator/JavaStructures.go @@ -39,12 +39,12 @@ type JavaClass struct { func NewJavaClass(name string, description string, javaPackage *JavaPackage, dataMembers []*DataMember, requiredMembers []*RequiredMember, constantDataMembers []*DataMember, hasSerializedNameVar bool) *JavaClass { javaClass := JavaClass{ - Name: name, - Description: SplitDescription(description), - JavaPackage: javaPackage, - DataMembers: dataMembers, - RequiredMembers: requiredMembers, - ConstantDataMembers: constantDataMembers, + Name: name, + Description: SplitDescription(description), + JavaPackage: javaPackage, + DataMembers: dataMembers, + RequiredMembers: requiredMembers, + ConstantDataMembers: constantDataMembers, HasSerializedNameVar: hasSerializedNameVar, } javaClass.Sort() @@ -99,20 +99,24 @@ func NewDataMember(name string, memberType string, description string) *DataMemb if isSnakeCase(name) { serializedOverrideName = name } - dataMember := DataMember { - Name: utils.StringToCamel(name), - PascalCaseName: utils.StringToPascal(name), - MemberType: memberType, - Description: SplitDescription(description), + + // If the name is kebab-case (eg: my-variable) then lets turn it into + // snake-case (eg: my_variable) which we can then turn easily into the other + // cases. + name = strings.ReplaceAll(name, "-", "_") + + dataMember := DataMember{ + Name: utils.StringToCamel(name), + PascalCaseName: utils.StringToPascal(name), + MemberType: memberType, + Description: SplitDescription(description), SerializedNameOverride: serializedOverrideName, } return &dataMember } func isSnakeCase(name string) bool { - var isSnakeCase bool - wordArray := strings.Split(name, "_") - isSnakeCase = len(wordArray) > 1 + var isSnakeCase bool = strings.Contains(name, "_") || strings.Contains(name, "-") return isSnakeCase } @@ -161,14 +165,14 @@ func stringArrayToEnumValues(stringEnums []string) []EnumValue { for _, value := range stringEnums { var constantFormatName string var stringFormat string - if value != "nil"{ + if value != "nil" { constantFormatName = utils.StringToScreamingSnake(value) stringFormat = value - enumValue := EnumValue { + enumValue := EnumValue{ ConstFormatName: constantFormatName, - StringFormat: stringFormat, + StringFormat: stringFormat, } - + enumValues = append(enumValues, enumValue) } } diff --git a/openapi2beans/pkg/generator/yaml2java_test.go b/openapi2beans/pkg/generator/yaml2java_test.go index 2584844..44c13d4 100644 --- a/openapi2beans/pkg/generator/yaml2java_test.go +++ b/openapi2beans/pkg/generator/yaml2java_test.go @@ -979,6 +979,46 @@ components: assert.Contains(t, generatedClassFile, varCreation) } +func TestGenerateFilesProducesClassWithDashInPropertyName(t *testing.T) { + // Given... + packageName := "generated" + mockFileSystem := files.NewMockFileSystem() + projectFilepath := "dev/wyvinar" + apiFilePath := "test-resources/single-bean.yaml" + objectName := "MyBeanName" + generatedCodeFilePath := "dev/wyvinar/generated/MyBeanName.java" + apiYaml := `openapi: 3.0.3 +components: + schemas: + MyBeanName: + type: object + description: A simple example + properties: + my-string-var: + type: string +` + // When... + mockFileSystem.WriteTextFile(apiFilePath, apiYaml) + + // When... + err := GenerateFiles(mockFileSystem, projectFilepath, apiFilePath, packageName, true) + + // Then... + assert.Nil(t, err) + generatedClassFile := openGeneratedFile(t, mockFileSystem, generatedCodeFilePath) + assertClassFileGeneratedOk(t, generatedClassFile, objectName) + getter := `public String getMyStringVar() { + return this.myStringVar; + }` + setter := `public void setMyStringVar(String myStringVar) { + this.myStringVar = myStringVar; + }` + varCreation := `private String myStringVar;` + assert.Contains(t, generatedClassFile, getter) + assert.Contains(t, generatedClassFile, setter) + assert.Contains(t, generatedClassFile, varCreation) +} + func TestGenerateFilesProducesClassWithReferencedArrayProperty(t *testing.T) { // Given... packageName := "generated" @@ -1343,4 +1383,4 @@ func TestRetrieveAllJavaFilesDoesntPickUpDeepJavaFile(t *testing.T) { assert.Equal(t, 1, len(javafilepaths)) assert.Contains(t, javafilepaths, randomFilepath) assert.NotContains(t, javafilepaths, randomFilepath2) -} \ No newline at end of file +}