From 25588b83146b315686dc0ae88c866c0613f50348 Mon Sep 17 00:00:00 2001 From: Dean Date: Mon, 10 Oct 2022 12:59:42 +0100 Subject: [PATCH 1/3] enumUnknownDefaultCase true added for Spring --- .../resources/JavaSpring/enumClass.mustache | 2 +- .../JavaSpring/enumOuterClass.mustache | 2 +- .../java/spring/SpringCodegenTest.java | 26 +++++++++++++++++ .../src/test/resources/bugs/issue_13241.yaml | 28 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/enumClass.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/enumClass.mustache index 59fa30eca3e5..19a7de4a1bbb 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/enumClass.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/enumClass.mustache @@ -45,6 +45,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 0c1156aa15ea..e14069e0fe16 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/enumOuterClass.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/enumOuterClass.mustache @@ -46,6 +46,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 ac1f12a3eb0f..850cfd37d6a1 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 @@ -1696,4 +1696,30 @@ public void shouldNotUseEqualsNullableForArrayWhenNotSetInConfig_issue13385() th .assertMethod("equals") .bodyContainsLines("return Arrays.equals(this.picture, testObject.picture);"); } + + @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")).printFileContent() + .assertMethod("fromValue").bodyContainsLines("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")).printFileContent() + .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..78771207c0b4 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml @@ -0,0 +1,28 @@ +openapi: 3.0.3 +info: + title: Title + version: "1" +paths: + /test: + get: + responses: + 200: + description: Success + content: + application/json: + schema: + $ref: '#/components/schemas/TestObject' +components: + schemas: + TestObject: + type: object + properties: + nullableProperty: + type: string + nullable: true + readOnly: true + notNullableProperty: + type: string + readOnly: true + notNullablePropertyNotRO: + type: integer From 4c3adbd4af3baff17373e555949749dde8f2b41b Mon Sep 17 00:00:00 2001 From: Dean Date: Mon, 10 Oct 2022 13:00:14 +0100 Subject: [PATCH 2/3] Resource added for test --- .../src/test/resources/bugs/issue_13241.yaml | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml b/modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml index 78771207c0b4..b30b61f19183 100644 --- a/modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml +++ b/modules/openapi-generator/src/test/resources/bugs/issue_13241.yaml @@ -1,28 +1,17 @@ -openapi: 3.0.3 +openapi: "3.0.0" info: - title: Title - version: "1" + version: 2.0.0 + title: test paths: - /test: + /pets: get: + summary: List all pets + operationId: listPets responses: - 200: - description: Success - content: - application/json: - schema: - $ref: '#/components/schemas/TestObject' + '200': + description: OK components: schemas: - TestObject: - type: object - properties: - nullableProperty: - type: string - nullable: true - readOnly: true - notNullableProperty: - type: string - readOnly: true - notNullablePropertyNotRO: - type: integer + Color: + type: string + enum: [RED, BLUE, GREEN] \ No newline at end of file From afd05055a265ca59b182c188f35eb60c9e112f58 Mon Sep 17 00:00:00 2001 From: Dean Date: Wed, 12 Oct 2022 10:10:58 +0100 Subject: [PATCH 3/3] Updated tests --- .../openapitools/codegen/java/spring/SpringCodegenTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 850cfd37d6a1..32163c55079a 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 @@ -1706,8 +1706,8 @@ public void testEnumUnknownDefaultCaseDeserializationTrue_issue13241() throws IO Map files = generateFiles(codegen, "src/test/resources/bugs/issue_13241.yaml"); - JavaFileAssert.assertThat(files.get("Color.java")).printFileContent() - .assertMethod("fromValue").bodyContainsLines("UNKNOWN_DEFAULT_OPEN_API"); + JavaFileAssert.assertThat(files.get("Color.java")) + .assertMethod("fromValue").bodyContainsLines("return UNKNOWN_DEFAULT_OPEN_API"); } @Test @@ -1717,7 +1717,7 @@ public void testEnumUnknownDefaultCaseDeserializationNotSet_issue13241() throws codegen.setLibrary(SPRING_BOOT); Map files = generateFiles(codegen, "src/test/resources/bugs/issue_13241.yaml"); - JavaFileAssert.assertThat(files.get("Color.java")).printFileContent() + JavaFileAssert.assertThat(files.get("Color.java")) .assertMethod("fromValue").bodyContainsLines("throw new IllegalArgumentException(\"Unexpected value '\" + value + \"'\");"); }