diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/enumClass.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/enumClass.mustache index 943a6822273f..c148ee898d34 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/enumClass.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/enumClass.mustache @@ -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}} } } diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/enumOuterClass.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/enumOuterClass.mustache index 24c1057d9a8f..12c679ba993f 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/enumOuterClass.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/enumOuterClass.mustache @@ -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}} } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java index 48fef733c943..cd7c21512dc9 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java @@ -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 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 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 + \"'\");"); + + } } diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml b/modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml new file mode 100644 index 000000000000..b30b61f19183 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml @@ -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] \ No newline at end of file