swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[JAVA] "allOf" with an enum in schema does not generate in Java properly

Open senahornby opened this issue 3 years ago • 2 comments

Description

I am working with a third party to convert their schema to Java. Unfortunately, the way they have done their enums has rendered them nearly unusable in Java. Please note that I cannot alter this schema. While I have played around with it for debugging purposes, I ultimately cannot change it for production.

Swagger-codegen version

3.034

Swagger declaration file content or url

The third party has provided both a json and yml version of their schema with equal results on my part.

  schemas:
    Employee:
      type: object
      properties:
        EmployeeGender:
          $ref: '#/components/schemas/Gender'
    Gender:
      allOf:
        - type: string
        - enum:
            - Female
            - Male

YAML

And here is the same thing in JSON:

"components": {
        "schemas": {
          "Employee": {
                "type": "object",
                "properties": {
                    "EmployeeGender": {"$ref": "#/components/schemas/Gender"}
                }
          },
          "Gender": {
                "allOf": [
                    {"type": "string"},
                    {
                        "enum": [
                            "Female",
                            "Male"
                        ]
                    }
                ]
            }
        }

JSON

It then presents me with Java that has no usable enums.

public class EmployeeGender {

  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    return true;
  }

  @Override
  public int hashCode() {
    return Objects.hash();
  }


  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class EmployeeGender {\n");
    
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(java.lang.Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }

}

Obviously I would rather have something like:

public enum EmployeeGender {
  Female("Female"),
  Male("Male");
Command line used for generation

I have not used the command line, as this is generated through maven using Java 17. I will paste the relevant properties in my pom.xml:

        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.13.3</version>
        </dependency>

And the plugin:

            <plugin>
                <groupId>io.swagger.codegen.v3</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>3.0.34</version>
                <executions>
                    <execution>
                        <id>aproject</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <inputSpec>${project.basedir}/resources/schema.yml</inputSpec>
                    <language>java</language>
                    <library>resttemplate</library>
                    <generateApis>false</generateApis>
                    <apiPackage>this.is.a.package</apiPackage>
                    <modelPackage>this.is.a.package</modelPackage>
                    <invokerPackage>this.is.a.package</invokerPackage>
                    <generateApiTests>false</generateApiTests>
                    <generateApiDocumentation>false</generateApiDocumentation>
                    <generateModelTests>false</generateModelTests>
                    <generateModelDocumentation>false</generateModelDocumentation>
                    <generateSupportingFiles>true</generateSupportingFiles>
                    <configOptions>
                        <interfaceOnly>true</interfaceOnly>
                        <dateLibrary>legacy</dateLibrary>
                    </configOptions>
                </configuration>
            </plugin>

I then run a mvn clean swagger-codegen:generate or maven clean compile or whatever I need.

Steps to reproduce
  1. Set up schema as shown and pom.xml as shown, either using the json or yml file for generation. (Both have the same results for me)
  2. Run mvn clean swagger-codegen:generate or maven clean compile or any other maven command that would generate the code.
  3. Check the relevant generated Java files. I would expect the enums to generate enum Java files instead of whatever mess they end up generating.
Related issues/PRs

Issue 1746 Issue 2450 Issue 10446

Suggest a fix/enhancement

From my testing and messing with the schemas, removing the "allOf" from the enum in the schema will produce the Java class I am looking for. However, I cannot alter the schema for production. I need a fix within the code generation itself.

senahornby avatar Jun 28 '22 16:06 senahornby

Hi Is there any resolution for this? I have similar issue where we are getting about 7k files generated with enum name as String and that too twice in the same file, due to which there are tons of compilation errors.

edigeek avatar Oct 13 '22 18:10 edigeek

We are currently facing the same problem. Any progress?

gianluca-moro avatar Apr 08 '24 11:04 gianluca-moro