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

Commit

Permalink
Fixes to allow gson to correctly serialise constants and enums (#72)
Browse files Browse the repository at this point in the history
* fix: Change generated Java constants to be non-static to allow serialization

Signed-off-by: Eamonn Mansour <[email protected]>

* fix: Use outputName strings instead of constants when converting enums to JSON

Signed-off-by: Eamonn Mansour <[email protected]>

---------

Signed-off-by: Eamonn Mansour <[email protected]>
  • Loading branch information
eamansour authored Oct 24, 2024
1 parent e1fd7e2 commit 01cd188
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
13 changes: 12 additions & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,18 @@
"name": "TwilioKeyDetector"
}
],
"results": {},
"results": {
"certificates/ibminter.pem": [
{
"hashed_secret": "b3723414cb4a90ac8c2bc504ea01923fe5fccc8a",
"is_secret": false,
"is_verified": false,
"line_number": 28,
"type": "Artifactory Credentials",
"verified_result": null
}
]
},
"version": "0.13.1+ibm.62.dss",
"word_list": {
"file": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public void TestCanSerialiseTheEnumWithValue1() throws Exception {
BeanWithEnumPropertyAnEnumProperty enumUnderTest = BeanWithEnumPropertyAnEnumProperty.STRING_1;
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String serialisedForm = gson.toJson(enumUnderTest);
assertThat(serialisedForm).contains("\"STRING_1\"");
assertThat(serialisedForm).contains("\"string1\"");
}

@Test
public void TestCanSerialiseTheEnumWithValue2() throws Exception {
BeanWithEnumPropertyAnEnumProperty enumUnderTest = BeanWithEnumPropertyAnEnumProperty.STRING_2;
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String serialisedForm = gson.toJson(enumUnderTest);
assertThat(serialisedForm).contains("\"STRING_2\"");
assertThat(serialisedForm).contains("\"string2\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public void TestCanSerialiseTheBean() throws Exception {
BeanWithEnumProperty beanUnderTest = new BeanWithEnumProperty(enumProperty);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String serialisedForm = gson.toJson(beanUnderTest);
assertThat(serialisedForm).contains("\"anEnumProperty\": \"STRING_1\"");
assertThat(serialisedForm).contains("\"anEnumProperty\": \"string1\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public class {{Name}} {
{{#Description}}
// {{&.}}
{{/Description}}
public static final {{MemberType}} {{Name}}{{#ConstantVal}} = {{&.}}{{/ConstantVal}};
public final {{MemberType}} {{Name}}{{#ConstantVal}} = {{&.}}{{/ConstantVal}};
{{/ConstantDataMembers}}

public {{Name}} ({{#RequiredMembers}}{{^IsFirst}}, {{/IsFirst}}{{DataMember.MemberType}} {{DataMember.Name}}{{/RequiredMembers}}) {
public {{Name}}({{#RequiredMembers}}{{^IsFirst}}, {{/IsFirst}}{{DataMember.MemberType}} {{DataMember.Name}}{{/RequiredMembers}}) {
{{#RequiredMembers}}
this.{{DataMember.Name}} = {{DataMember.Name}};
{{/RequiredMembers}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
*/
package {{JavaPackage.Name}};

import com.google.gson.annotations.SerializedName;

{{#Description}}
// {{&.}}
{{/Description}}
public enum {{Name}} {
{{#EnumValues}}
{{ConstFormatName}}{{#StringFormat}} ("{{&.}}"){{/StringFormat}}{{^IsFinal}},{{/IsFinal}}{{#IsFinal}};{{/IsFinal}}
{{#StringFormat}}@SerializedName("{{&.}}"){{/StringFormat}}
{{ConstFormatName}}{{#StringFormat}}("{{&.}}"){{/StringFormat}}{{^IsFinal}},
{{/IsFinal}}{{#IsFinal}};{{/IsFinal}}
{{/EnumValues}}

private final String outputName;
Expand Down
8 changes: 4 additions & 4 deletions openapi2beans/pkg/generator/package2java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func openGeneratedFile(t *testing.T, mockFileSystem files.FileSystem, generatedC
func assertClassFileGeneratedOk(t *testing.T, generatedFile string, className string) {
assert.Contains(t, generatedFile, "package "+TARGET_JAVA_PACKAGE)
assert.Contains(t, generatedFile, "public class "+className)
assert.Contains(t, generatedFile, "public "+className+" (")
assert.Contains(t, generatedFile, "public "+className+"(")
}

func assertVariablesGeneratedOk(t *testing.T, generatedFile string, dataMembers []*DataMember) {
Expand All @@ -74,7 +74,7 @@ func assertVariablesGeneratedOk(t *testing.T, generatedFile string, dataMembers

func assertConstantsGeneratedOk(t *testing.T, generatedFile string, constDataMembers []*DataMember) {
for _, constDataMember := range constDataMembers {
assert.Contains(t, generatedFile, "public static final "+constDataMember.MemberType+" "+constDataMember.Name+" = "+constDataMember.ConstantVal)
assert.Contains(t, generatedFile, "public final "+constDataMember.MemberType+" "+constDataMember.Name+" = "+constDataMember.ConstantVal)
for _, line := range constDataMember.Description {
assert.Contains(t, generatedFile, "// "+line)
}
Expand All @@ -84,7 +84,7 @@ func assertConstantsGeneratedOk(t *testing.T, generatedFile string, constDataMem
func assertEnumFileGeneratedOk(t *testing.T, generatedFile string, javaEnum *JavaEnum) {
assert.Contains(t, generatedFile, "package "+TARGET_JAVA_PACKAGE)
assert.Contains(t, generatedFile, "public enum "+javaEnum.Name)
valueTemplate := `%s ("%s"),`
valueTemplate := `%s("%s"),`

for _, value := range javaEnum.EnumValues {
assert.Contains(t, generatedFile, fmt.Sprintf(valueTemplate, value.ConstFormatName, value.StringFormat))
Expand Down Expand Up @@ -344,7 +344,7 @@ func TestPackageStructParsesToTemplateWithClassWithRequiredProperty(t *testing.T
generatedFile := openGeneratedFile(t, mockFileSystem, generatedCodeFilePath)
assertClassFileGeneratedOk(t, generatedFile, className)
assertVariablesGeneratedOk(t, generatedFile, dataMembers)
constructor := `public MyBean (String RandMember1) {
constructor := `public MyBean(String RandMember1) {
this.RandMember1 = RandMember1;
}`
assert.Contains(t, generatedFile, constructor)
Expand Down
22 changes: 13 additions & 9 deletions openapi2beans/pkg/generator/yaml2java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ components:
assert.Contains(t, generatedClassFile, getter)
assert.Contains(t, generatedClassFile, setter)
assert.Contains(t, generatedClassFile, varCreation)
assert.Contains(t, generatedClassFile, ` public MyBeanName (String myStringVar) {
assert.Contains(t, generatedClassFile, ` public MyBeanName(String myStringVar) {
this.myStringVar = myStringVar;
}`)
}
Expand Down Expand Up @@ -469,7 +469,7 @@ components:
assert.Contains(t, generatedClassFile, getter)
assert.Contains(t, generatedClassFile, setter)
assert.Contains(t, generatedClassFile, varCreation)
assert.Contains(t, generatedClassFile, ` public MyBeanName (int myIntVar, String myStringVar) {
assert.Contains(t, generatedClassFile, ` public MyBeanName(int myIntVar, String myStringVar) {
this.myIntVar = myIntVar;
this.myStringVar = myStringVar;
}`)
Expand Down Expand Up @@ -529,7 +529,7 @@ components:
assert.Contains(t, generatedClassFile, getter)
assert.Contains(t, generatedClassFile, setter)
assert.Contains(t, generatedClassFile, varCreation)
assert.Contains(t, generatedClassFile, ` public MyBeanName (String myStringVar) {
assert.Contains(t, generatedClassFile, ` public MyBeanName(String myStringVar) {
this.myStringVar = myStringVar;
}`)
}
Expand Down Expand Up @@ -841,13 +841,16 @@ components:
assert.Contains(t, generatedClassFile, getter)
assert.Contains(t, generatedClassFile, setter)
assert.Contains(t, generatedClassFile, varCreation)
assert.Contains(t, generatedClassFile, ` public MyBeanName (MyBeanNameMyEnum myEnum) {
assert.Contains(t, generatedClassFile, ` public MyBeanName(MyBeanNameMyEnum myEnum) {
this.myEnum = myEnum;
}`)
generatedEnumFile := openGeneratedFile(t, mockFileSystem, "dev/wyvinar/generated/MyBeanNameMyEnum.java")
expectedEnumFile := `public enum MyBeanNameMyEnum {
STRING_1 ("string1"),
STRING_2 ("string2");
@SerializedName("string1")
STRING_1("string1"),
@SerializedName("string2")
STRING_2("string2");
%s
}`
Expand Down Expand Up @@ -893,11 +896,12 @@ components:
assert.Contains(t, generatedClassFile, getter)
assert.Contains(t, generatedClassFile, setter)
assert.Contains(t, generatedClassFile, varCreation)
assert.Contains(t, generatedClassFile, ` public MyBeanName () {
assert.Contains(t, generatedClassFile, ` public MyBeanName() {
}`)
generatedEnumFile := openGeneratedFile(t, mockFileSystem, "dev/wyvinar/generated/MyBeanNameMyEnum.java")
expectedEnumFile := `public enum MyBeanNameMyEnum {
RAND_VALUE_1 ("randValue1");
@SerializedName("randValue1")
RAND_VALUE_1("randValue1");
%s
}`
Expand Down Expand Up @@ -933,7 +937,7 @@ components:
assert.Nil(t, err)
generatedClassFile := openGeneratedFile(t, mockFileSystem, generatedCodeFilePath)
assertClassFileGeneratedOk(t, generatedClassFile, objectName)
constAssignment := `public static final String MY_CONST_VAR = "constVal"`
constAssignment := `public final String MY_CONST_VAR = "constVal"`
assert.Contains(t, generatedClassFile, constAssignment)
}

Expand Down

0 comments on commit 01cd188

Please sign in to comment.