From 940cab5d77291b0bfb32bab7993d2c6fd6169e95 Mon Sep 17 00:00:00 2001 From: Bragolgirith <6455473+Bragolgirith@users.noreply.github.com> Date: Tue, 20 Aug 2024 22:17:41 +0200 Subject: [PATCH] [Java] Fix compilation for a map of enum --- .../openapitools/codegen/DefaultCodegen.java | 2 +- .../codegen/java/JavaClientCodegenTest.java | 17 +++++++ .../3_0/issue_19393_map_of_enum.yaml | 51 +++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/issue_19393_map_of_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 eaedf606cade..1697f7071a76 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 @@ -4275,7 +4275,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 a16014271aa5..1da909b68dd3 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 @@ -2203,6 +2203,23 @@ public void shouldNotAddAdditionalModelAnnotationsToAbstractOpenApiSchema_issue1 .bodyContainsLines("if (b.value.equals(value)) {"); } + @Test public void testMapOfEnum_issue19393() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_19393_map_of_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("Employee.java")) + .assertProperty("projectRole") + .withType("Map") + .toType() + .assertProperty("projectRoles") + .withType("Map>"); + } + @Test public void testWebClientResponseTypeWithUseAbstractionForFiles_issue16589() { final Path output = newTempFolder(); final CodegenConfigurator configurator = new CodegenConfigurator() diff --git a/modules/openapi-generator/src/test/resources/3_0/issue_19393_map_of_enum.yaml b/modules/openapi-generator/src/test/resources/3_0/issue_19393_map_of_enum.yaml new file mode 100644 index 000000000000..58f93e87057f --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/issue_19393_map_of_enum.yaml @@ -0,0 +1,51 @@ +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: + /employees/{employeeId}: + put: + operationId: updateEmployee + parameters: + - name: employeeId + in: path + required: true + schema: + type: string + format: uuid + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Employee' + required: true + responses: + '200': + description: OK +components: + schemas: + Employee: + type: object + properties: + projectRole: + type: object + additionalProperties: + type: string + enum: + - DEVELOPER + - TESTER + - OWNER + projectRoles: + type: object + additionalProperties: + uniqueItems: true + type: array + items: + type: string + enum: + - DEVELOPER + - TESTER + - OWNER