Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java][Spring] enumUnknownDefaultCase true now returns correctly in fromValue #18914

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
return b;
}
}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}{{#allowableValues}}{{#enumVars}}{{#-last}}return {{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/enumUnknownDefaultCase}}{{/isNullable}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public enum {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatyp
return b;
}
}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}{{#allowableValues}}{{#enumVars}}{{#-last}}return {{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/enumUnknownDefaultCase}}{{/isNullable}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4865,4 +4865,29 @@ public void shouldGenerateOptionalParameterTypesWhenUsingOptionalAndDelegate_iss
.toMethod()
.toFileAssert();
}

@Test
public void testEnumUnknownDefaultCaseDeserializationTrue_issue13241() throws IOException {

SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
codegen.additionalProperties().put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, "true");

Map<String, File> files = generateFiles(codegen, "src/test/resources/bugs/issue_13241.yaml");

JavaFileAssert.assertThat(files.get("Color.java"))
.assertMethod("fromValue").bodyContainsLines("return UNKNOWN_DEFAULT_OPEN_API");
}

@Test
public void testEnumUnknownDefaultCaseDeserializationNotSet_issue13241() throws IOException {

SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
Map<String, File> files = generateFiles(codegen, "src/test/resources/bugs/issue_13241.yaml");

JavaFileAssert.assertThat(files.get("Color.java"))
.assertMethod("fromValue").bodyContainsLines("throw new IllegalArgumentException(\"Unexpected value '\" + value + \"'\");");

}
}
17 changes: 17 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: "3.0.0"
info:
version: 2.0.0
title: test
paths:
/pets:
get:
summary: List all pets
operationId: listPets
responses:
'200':
description: OK
components:
schemas:
Color:
type: string
enum: [RED, BLUE, GREEN]
Loading