From 8ce332a540f3ece6779bd90d01c73223f5c8edde Mon Sep 17 00:00:00 2001 From: Bragolgirith <6455473+Bragolgirith@users.noreply.github.com> Date: Sun, 24 Nov 2024 17:40:44 +0100 Subject: [PATCH] [Java] Fix compilation for Map of InnerEnum (#19401) --- .../openapitools/codegen/DefaultCodegen.java | 2 +- .../codegen/java/JavaClientCodegenTest.java | 19 +++++++++ .../3_0/issue_19393_map_of_inner_enum.yaml | 40 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/issue_19393_map_of_inner_enum.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index b42858a24945..d8b577c93cae 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -4323,7 +4323,7 @@ protected void updateDataTypeWithEnumForMap(CodegenProperty property) { if (baseItem != null) { // set both datatype and datetypeWithEnum as only the inner type is enum - property.datatypeWithEnum = property.datatypeWithEnum.replace(", " + baseItem.baseType, ", " + toEnumName(baseItem)); + property.datatypeWithEnum = property.datatypeWithEnum.replace(baseItem.baseType + ">", toEnumName(baseItem) + ">"); // naming the enum with respect to the language enum naming convention // e.g. remove [], {} from array/map of enum diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java index 6789b897591a..8fd833696822 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java @@ -2317,6 +2317,25 @@ public void testEnumCaseSensitive_issue8084() { .bodyContainsLines("if (b.value.equals(value)) {"); } + @Test + public void testMapOfInnerEnum_issue19393() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_19393_map_of_inner_enum.yaml"); + final JavaClientCodegen codegen = new JavaClientCodegen(); + codegen.setOpenAPI(openAPI); + codegen.setOutputDir(newTempFolder().toString()); + + Map files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen)) + .generate().stream().collect(Collectors.toMap(File::getName, Function.identity())); + + JavaFileAssert.assertThat(files.get("EmployeeWithMapOfEnum.java")) + .assertProperty("projectRole") + .withType("Map"); + + JavaFileAssert.assertThat(files.get("EmployeeWithMultiMapOfEnum.java")) + .assertProperty("projectRoles") + .withType("Map>"); + } + @Test public void testWebClientResponseTypeWithUseAbstractionForFiles_issue16589() { final Path output = newTempFolder(); diff --git a/modules/openapi-generator/src/test/resources/3_0/issue_19393_map_of_inner_enum.yaml b/modules/openapi-generator/src/test/resources/3_0/issue_19393_map_of_inner_enum.yaml new file mode 100644 index 000000000000..28c81333c97e --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/issue_19393_map_of_inner_enum.yaml @@ -0,0 +1,40 @@ +openapi: 3.0.0 +info: + version: 1.0.0 + title: OpenAPI Test API + license: + name: Apache-2.0 + url: 'https://www.apache.org/licenses/LICENSE-2.0.html' +paths: + /status: + get: + responses: + '200': + description: desc +components: + schemas: + EmployeeWithMapOfEnum: + type: object + properties: + projectRole: + type: object + additionalProperties: + type: string + enum: + - DEVELOPER + - TESTER + - OWNER + EmployeeWithMultiMapOfEnum: + type: object + properties: + projectRoles: + type: object + additionalProperties: + uniqueItems: true + type: array + items: + type: string + enum: + - DEVELOPER + - TESTER + - OWNER