Skip to content

Commit

Permalink
Fixed Java enums not generating accurate value types, defaulting to `…
Browse files Browse the repository at this point in the history
…Object` to support all cases
  • Loading branch information
jonaslagoni committed Jul 28, 2022
1 parent 17149e9 commit ff3f5d4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
12 changes: 7 additions & 5 deletions src/generators/java/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ export const JAVA_DEFAULT_ENUM_PRESET: EnumPresetType<JavaOptions> = {
return `${item.key}(${item.value})`;
},
additionalContent({ model }) {
return `private ${model.type} value;
const enumValueType = 'Object';

${model.type}(${model.type} value) {
return `private ${enumValueType} value;
${model.type}(${enumValueType} value) {
this.value = value;
}
@JsonValue
public ${model.type} getValue() {
public ${enumValueType} getValue() {
return value;
}
Expand All @@ -63,7 +65,7 @@ public String toString() {
}
@JsonCreator
public static ${model.type} fromValue(${model.type} value) {
public static ${model.type} fromValue(${enumValueType} value) {
for (${model.type} e : ${model.type}.values()) {
if (e.value.equals(value)) {
return e;
Expand Down
50 changes: 25 additions & 25 deletions test/generators/java/__snapshots__/JavaGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ exports[`JavaGenerator should render \`enum\` type (integer type) 1`] = `
"public enum Numbers {
NUMBER_0(0), NUMBER_1(1), NUMBER_2(2), NUMBER_3(3);
private Numbers value;
private Object value;
Numbers(Numbers value) {
Numbers(Object value) {
this.value = value;
}
@JsonValue
public Numbers getValue() {
public Object getValue() {
return value;
}
Expand All @@ -71,7 +71,7 @@ exports[`JavaGenerator should render \`enum\` type (integer type) 1`] = `
}
@JsonCreator
public static Numbers fromValue(Numbers value) {
public static Numbers fromValue(Object value) {
for (Numbers e : Numbers.values()) {
if (e.value.equals(value)) {
return e;
Expand All @@ -86,14 +86,14 @@ exports[`JavaGenerator should render \`enum\` type (string type) 1`] = `
"public enum States {
TEXAS(\\"Texas\\"), ALABAMA(\\"Alabama\\"), CALIFORNIA(\\"California\\"), NEW_YORK(\\"New York\\");
private States value;
private Object value;
States(States value) {
States(Object value) {
this.value = value;
}
@JsonValue
public States getValue() {
public Object getValue() {
return value;
}
Expand All @@ -103,7 +103,7 @@ exports[`JavaGenerator should render \`enum\` type (string type) 1`] = `
}
@JsonCreator
public static States fromValue(States value) {
public static States fromValue(Object value) {
for (States e : States.values()) {
if (e.value.equals(value)) {
return e;
Expand All @@ -118,14 +118,14 @@ exports[`JavaGenerator should render \`enum\` type (union type) 1`] = `
"public enum Union {
TEXAS(\\"Texas\\"), ALABAMA(\\"Alabama\\"), NUMBER_0(0), NUMBER_1(1), RESERVED_NUMBER_1(\\"1\\"), TRUE(\\"true\\"), CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_QUOTATION_TEST_QUOTATION_CURLYRIGHT(\\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\");
private Union value;
private Object value;
Union(Union value) {
Union(Object value) {
this.value = value;
}
@JsonValue
public Union getValue() {
public Object getValue() {
return value;
}
Expand All @@ -135,7 +135,7 @@ exports[`JavaGenerator should render \`enum\` type (union type) 1`] = `
}
@JsonCreator
public static Union fromValue(Union value) {
public static Union fromValue(Object value) {
for (Union e : Union.values()) {
if (e.value.equals(value)) {
return e;
Expand All @@ -160,14 +160,14 @@ exports[`JavaGenerator should render custom preset for \`enum\` type 1`] = `
public enum CustomEnum {
TEXAS(\\"Texas\\"), ALABAMA(\\"Alabama\\"), CALIFORNIA(\\"California\\");
private CustomEnum value;
private Object value;
CustomEnum(CustomEnum value) {
CustomEnum(Object value) {
this.value = value;
}
@JsonValue
public CustomEnum getValue() {
public Object getValue() {
return value;
}
Expand All @@ -177,7 +177,7 @@ public enum CustomEnum {
}
@JsonCreator
public static CustomEnum fromValue(CustomEnum value) {
public static CustomEnum fromValue(Object value) {
for (CustomEnum e : CustomEnum.values()) {
if (e.value.equals(value)) {
return e;
Expand All @@ -192,14 +192,14 @@ exports[`JavaGenerator should render enums with translated special characters 1`
"public enum States {
TEST_PLUS(\\"test+\\"), TEST(\\"test\\"), TEST_MINUS(\\"test-\\"), TEST_QUESTION_EXCLAMATION(\\"test?!\\"), ASTERISK_TEST(\\"*test\\");
private States value;
private Object value;
States(States value) {
States(Object value) {
this.value = value;
}
@JsonValue
public States getValue() {
public Object getValue() {
return value;
}
Expand All @@ -209,7 +209,7 @@ exports[`JavaGenerator should render enums with translated special characters 1`
}
@JsonCreator
public static States fromValue(States value) {
public static States fromValue(Object value) {
for (States e : States.values()) {
if (e.value.equals(value)) {
return e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ exports[`JAVA_DESCRIPTION_PRESET should render description and examples for enum
* Description for enum
* Examples: value
*/
public enum Enum {
public enum ReservedEnum {
ON(\\"on\\"), OFF(\\"off\\");
private Enum value;
private Object value;
Enum(Enum value) {
ReservedEnum(Object value) {
this.value = value;
}
@JsonValue
public Enum getValue() {
public Object getValue() {
return value;
}
Expand All @@ -46,8 +46,8 @@ public enum Enum {
}
@JsonCreator
public static Enum fromValue(Enum value) {
for (Enum e : Enum.values()) {
public static ReservedEnum fromValue(Object value) {
for (ReservedEnum e : ReservedEnum.values()) {
if (e.value.equals(value)) {
return e;
}
Expand Down

0 comments on commit ff3f5d4

Please sign in to comment.