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

Commit

Permalink
fix: Use outputName strings instead of constants when converting enum…
Browse files Browse the repository at this point in the history
…s to JSON

Signed-off-by: Eamonn Mansour <[email protected]>
  • Loading branch information
eamansour committed Oct 23, 2024
1 parent 7d7d611 commit 4dea474
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 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 @@ -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
2 changes: 1 addition & 1 deletion openapi2beans/pkg/generator/package2java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions openapi2beans/pkg/generator/yaml2java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,11 @@ components:
}`)
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 @@ -897,7 +900,8 @@ components:
}`)
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

0 comments on commit 4dea474

Please sign in to comment.