Skip to content

[BUG][JAVA] Compilation error for a Map<String, Set<InnerEnum>> due to incompatible types #1353

Description

@wizwoz

The exact same problem can be found here, the exact same solution can also work.

OpenAPITools/openapi-generator#19393 (comment)

I have the following schema that describes the different roles an employee can have in different projects as a map of collection of enum, e.g.:

    Employee:
      type: object
      properties:
        projectRoles:
          type: object
          additionalProperties:
            uniqueItems: true
            type: array
            items:
              type: string
              enum:
                - DEVELOPER
                - TESTER
                - OWNER

This works correctly with most generators, however the java generator seems to have some trouble with it and generates an invalid Employee.java class:

public class Employee {

  // ... truncated

  // This should be Map<String, Set<InnerEnum>> instead
  private Map<String, Set<String>> projectRoles = new HashMap<>();

  // ... truncated

  public Employee putProjectRolesItem(String key, Set<InnerEnum> projectRolesItem) {
    if (this.projectRoles == null) {
      this.projectRoles = new HashMap<>();
    }
    this.projectRoles.put(key, projectRolesItem); // Compiler error here due to the mismatching types
    return this;
  }
}

The compiler error is as follows

incompatible types: java.util.Set<Employee.InnerEnum> cannot be converted to java.util.Set<java.lang.String>

Note that the issue only occurs for inline enums (i.e. InnerEnums); enum references work as expected.

I have tried the fix mentioned in the above repo and can confirm the same fix works for swagger-codegen. I would have put up an MR but I cannot push

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions