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] Fix compilation error for a Map<String, Set<InnerEnum>> due to incompatible types #19401

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,24 @@ public void shouldNotAddAdditionalModelAnnotationsToAbstractOpenApiSchema_issue1
.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<String, File> 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<String, InnerEnum>");

JavaFileAssert.assertThat(files.get("EmployeeWithMultiMapOfEnum.java"))
.assertProperty("projectRoles")
.withType("Map<String, Set<InnerEnum>>");
}

@Test public void testWebClientResponseTypeWithUseAbstractionForFiles_issue16589() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did a test with this spec but still got errors:

[ERROR] C:\Users\wing3\AppData\Local\Temp\java-enum\src\main\java\org\openapitools\client\model\Employee.java:[118,9] error: enum InnerEnum is already defined in class Employee
[ERROR] C:\Users\wing3\AppData\Local\Temp\java-enum\src\main\java\org\openapitools\client\JSON.java:[49,7] error: duplicate class: org.openapitools.client.JSON

can you please take a look?

command:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/issue_19393_map_of_inner_enum.yaml -o /tmp/java-enum/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry about that!

The errors seem to be due to a different bug where the same name (InnerEnum) gets generated for multiple enums within the same schema.

I've now updated the test spec to use two different schemas.