Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #71 from galasa-dev/mcobbett-bean-generator-handle…
Browse files Browse the repository at this point in the history
…-kebab-case

Openapi2beans needs to handle kebab-case variables in the yaml
  • Loading branch information
techcobweb authored Oct 17, 2024
2 parents 15e9c7a + d4ed68d commit e1fd7e2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
40 changes: 22 additions & 18 deletions openapi2beans/pkg/generator/JavaStructures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
Expand Down
42 changes: 41 additions & 1 deletion openapi2beans/pkg/generator/yaml2java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit e1fd7e2

Please sign in to comment.