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

Commit

Permalink
merged with main
Browse files Browse the repository at this point in the history
Signed-off-by: Holly Hunt <[email protected]>
  • Loading branch information
Wyvinar committed Jun 4, 2024
2 parents 77f4b1b + ceb5ad6 commit 6d724bb
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ components:
properties:
aStringVariable:
type: string

BeanWithSnakeCasePrimitiveProperty:
type: object
description: A bean with a single primitive property
properties:
a_string_variable:
type: string

BeanWithMultiplePrimitiveProperties:
type: object
Expand Down
11 changes: 9 additions & 2 deletions openapi2beans/pkg/embedded/templates/JavaClassTemplate.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
package {{JavaPackage.Name}};

{{#HasSerializedNameVar}}
import com.google.gson.annotations.SerializedName;
{{/HasSerializedNameVar}}

{{#Description}}
// {{&.}}
{{/Description}}
Expand All @@ -14,6 +18,9 @@ public class {{Name}} {
{{#Description}}
// {{&.}}
{{/Description}}
{{#SerializedNameOverride}}
@SerializedName("{{&.}}")
{{/SerializedNameOverride}}
private {{MemberType}} {{Name}};
{{/DataMembers}}

Expand All @@ -36,14 +43,14 @@ public class {{Name}} {

// Getters //
{{#DataMembers}}
public {{MemberType}} get{{CamelCaseName}}() {
public {{MemberType}} get{{PascalCaseName}}() {
return this.{{Name}};
}
{{/DataMembers}}

// Setters //
{{#DataMembers}}
public void set{{CamelCaseName}}({{MemberType}} {{Name}}) {
public void set{{PascalCaseName}}({{MemberType}} {{Name}}) {
this.{{Name}} = {{Name}};
}
{{/DataMembers}}
Expand Down
29 changes: 16 additions & 13 deletions openapi2beans/pkg/generator/JavaStructures.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ func NewJavaPackage(name string) *JavaPackage {
}

type JavaClass struct {
Name string
Description []string
JavaPackage *JavaPackage
DataMembers []*DataMember
RequiredMembers []*RequiredMember
ConstantDataMembers []*DataMember
Name string
Description []string
JavaPackage *JavaPackage
DataMembers []*DataMember
RequiredMembers []*RequiredMember
ConstantDataMembers []*DataMember
HasSerializedNameVar bool
}

func NewJavaClass(name string, description []string, javaPackage *JavaPackage, dataMembers []*DataMember, requiredMembers []*RequiredMember, constantDataMembers []*DataMember) *JavaClass {
func NewJavaClass(name string, description []string, javaPackage *JavaPackage, dataMembers []*DataMember, requiredMembers []*RequiredMember, constantDataMembers []*DataMember, hasSerializedNameVar bool) *JavaClass {
javaClass := JavaClass{
Name: name,
Description: description,
JavaPackage: javaPackage,
DataMembers: dataMembers,
RequiredMembers: requiredMembers,
ConstantDataMembers: constantDataMembers,
HasSerializedNameVar: hasSerializedNameVar,
}
return &javaClass
}
Expand All @@ -68,12 +70,13 @@ func (class JavaClass) Sort() {
}

type DataMember struct {
Name string
CamelCaseName string
MemberType string
Description []string
Required bool
ConstantVal string
Name string
PascalCaseName string
MemberType string
Description []string
Required bool
ConstantVal string
SerializedNameOverride string
}

func (dataMember DataMember) IsConstant() bool {
Expand Down
89 changes: 76 additions & 13 deletions openapi2beans/pkg/generator/package2java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func assertVariablesGeneratedOk(t *testing.T, generatedFile string, dataMembers
for _, line := range dataMember.Description {
assert.Contains(t, generatedFile, "// "+line)
}
assert.Contains(t, generatedFile, "public "+dataMember.MemberType+" get"+dataMember.CamelCaseName+"() {")
assert.Contains(t, generatedFile, "public "+dataMember.MemberType+" get"+dataMember.PascalCaseName+"() {")
assert.Contains(t, generatedFile, "this."+dataMember.Name+" = "+dataMember.Name)
assert.Contains(t, generatedFile, "public void set"+dataMember.CamelCaseName+"("+dataMember.MemberType+" "+dataMember.Name+") {")
assert.Contains(t, generatedFile, "public void set"+dataMember.PascalCaseName+"("+dataMember.MemberType+" "+dataMember.Name+") {")
assert.Contains(t, generatedFile, "this."+dataMember.Name+" = "+dataMember.Name)
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestPackageStructParsesToTemplate(t *testing.T) {
className := "MyBean"
var javaPackage JavaPackage
javaPackage.Name = TARGET_JAVA_PACKAGE
class := NewJavaClass(className, []string{}, &javaPackage, nil, nil, nil)
class := NewJavaClass(className, []string{}, &javaPackage, nil, nil, nil, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand All @@ -123,7 +123,7 @@ func TestPackageStructParsesToTemplateWithClassWithMember(t *testing.T) {
MemberType: "String",
}
dataMembers := []*DataMember{&dataMember}
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil)
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestPackageStructParsesToTemplateWithClassWithMultipleMembers(t *testing.T)
MemberType: "String",
}
dataMembers := []*DataMember{&dataMember1, &dataMember2}
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil)
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand All @@ -183,7 +183,7 @@ func TestPackageStructParsesToTemplateWithClassWithArrayDataMember(t *testing.T)
MemberType: "String[]",
}
dataMembers := []*DataMember{&dataMember1}
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil)
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand All @@ -210,7 +210,7 @@ func TestPackageStructParsesToTemplateWithClassWithMultiDimensionalArrayDataMemb
MemberType: "String[][]",
}
dataMembers := []*DataMember{&dataMember1}
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil)
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestPackageStructParsesToTemplateWithClassWithMixedArrayAndPrimitiveDataMem
MemberType: "String",
}
dataMembers := []*DataMember{&dataMember1, &dataMember2}
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil)
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand All @@ -270,7 +270,7 @@ func TestPackageStructParsesToTemplateWithClassWithReferencedClassType(t *testin
MemberType: "ReferencedClass",
}
dataMembers := []*DataMember{&dataMember1}
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil)
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand All @@ -297,7 +297,7 @@ func TestPackageStructParsesToTemplateWithClassWithArrayOfReferencedClassType(t
MemberType: "ReferencedClass[]",
}
dataMembers := []*DataMember{&dataMember1}
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil)
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand Down Expand Up @@ -331,7 +331,7 @@ func TestPackageStructParsesToTemplateWithClassWithRequiredProperty(t *testing.T
dataMembers = append(dataMembers, &dataMember1)
requiredMembers := []*RequiredMember{}
requiredMembers = append(requiredMembers, &requiredMember1)
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, requiredMembers, nil)
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, requiredMembers, nil, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestPackageStructWithClassWithReferenceToEnumParsesCorrectly(t *testing.T)
Required: true,
}
dataMembers := []*DataMember{dataMember}
class := NewJavaClass(className, classDesc, javaPackage, dataMembers, nil, nil)
class := NewJavaClass(className, classDesc, javaPackage, dataMembers, nil, nil, false)
javaPackage.Classes[className] = class
javaPackage.Enums[enumName] = &javaEnum
mockFileSystem := files.NewMockFileSystem()
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestPackageStructParsesToTemplateWithClassWithConstantMember(t *testing.T)
ConstantVal: "random string thing",
}
constDataMembers := []*DataMember{&constDataMember}
class := NewJavaClass(className, []string{}, &javaPackage, nil, nil, constDataMembers)
class := NewJavaClass(className, []string{}, &javaPackage, nil, nil, constDataMembers, false)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"
Expand All @@ -441,3 +441,66 @@ func TestPackageStructParsesToTemplateWithClassWithConstantMember(t *testing.T)
assertClassFileGeneratedOk(t, generatedFile, className)
assertConstantsGeneratedOk(t, generatedFile, constDataMembers)
}

func TestPackageStructParsesToTemplateWithClassWithMemberWithSerializedName(t *testing.T) {
// Given...
className := "MyBean"
var javaPackage JavaPackage
javaPackage.Name = TARGET_JAVA_PACKAGE
memberName := "rand_member"
dataMember := DataMember{
Name: "randMember",
PascalCaseName: "RandMember",
Description: []string{"random member for test purposes"},
MemberType: "String",
SerializedNameOverride: memberName,
}
hasSerialisedName := true
dataMembers := []*DataMember{&dataMember}
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil, hasSerialisedName)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"

// When...
err := createJavaClassFile(class, mockFileSystem, getEmbeddedClassTemplate(t), storeFilepath)

// Then...
assert.Nil(t, err)
generatedFile := openGeneratedFile(t, mockFileSystem, generatedCodeFilePath)
assertClassFileGeneratedOk(t, generatedFile, className)
assertVariablesGeneratedOk(t, generatedFile, dataMembers)
assert.Contains(t, generatedFile, "@SerializedName(\"rand_member\")")
assert.Contains(t, generatedFile, "import com.google.gson.annotations.SerializedName;")
}

func TestPackageStructParsesToTemplateWithClassWithMemberWithoutSerializedName(t *testing.T) {
// Given...
className := "MyBean"
var javaPackage JavaPackage
javaPackage.Name = TARGET_JAVA_PACKAGE
memberName := "randMember"
dataMember := DataMember{
Name: memberName,
PascalCaseName: "RandMember",
Description: []string{"random member for test purposes"},
MemberType: "String",
}
hasSerialisedName := false
dataMembers := []*DataMember{&dataMember}
class := NewJavaClass(className, []string{}, &javaPackage, dataMembers, nil, nil, hasSerialisedName)
mockFileSystem := files.NewMockFileSystem()
storeFilepath := "generated"
generatedCodeFilePath := storeFilepath + "/" + className + ".java"

// When...
err := createJavaClassFile(class, mockFileSystem, getEmbeddedClassTemplate(t), storeFilepath)

// Then...
assert.Nil(t, err)
generatedFile := openGeneratedFile(t, mockFileSystem, generatedCodeFilePath)
assertClassFileGeneratedOk(t, generatedFile, className)
assertVariablesGeneratedOk(t, generatedFile, dataMembers)
assert.NotContains(t, generatedFile, "@SerializedName(\"rand_member\")")
assert.NotContains(t, generatedFile, "import com.google.gson.annotations.SerializedName;")
}
Loading

0 comments on commit 6d724bb

Please sign in to comment.