swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.91k stars 6.03k forks source link

jaxrs-restasy-eap generates bad equals functions in some cases #10524

Open khaksnes opened 3 years ago

khaksnes commented 3 years ago
Description

We are using swagger-codegen-maven-plugin to generate Jackson mappings for a couple of our APIs We typically use this to support camel-restdsl-swagger-plugin The problem being that we do se a couple of serious bugs in the generated equals functions. The first one being that code on this form

@Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Relation relation = (Relation) o;
    return Objects.equals(relation, relation.relation) &&
        Objects.equals(descriptionL1, relation.descriptionL1) &&
        Objects.equals(descriptionL2, relation.descriptionL2);
  }

Is generated the offending test being the first one where relation is compared to relation.relation what should be compared is this.relation to relation.relation. In fact it would be safe and probably quite easy to add the this. prefix to the first argument of all 3 Object.equals calls.

The second problem is that the generated equal seems to be bad if binary data are part of the generated class added using

type: "string"
format: "byte"

In this case we do get code like this

@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } PersonAttachment personAttachment = (PersonAttachment) o; return Objects.equals(contentType, personAttachment.contentType) && Objects.equals(imageData, personAttachment.imageData); }

Where imageDate is of type byte[] Objects.equals does not work well in this case (and for arrays in general) since arrays do not override the equals method of Object. java.util.Arrays::equals should be used instead

Swagger-codegen version

We are using swagger-codegen-maven-plugin version 2.4.14 with language set to jaxrs-resteasy-eap with generateApis, generateApiTests, generateModelTests and generateSupportingFiles all set to false.

Swagger declaration file content or url

Our yaml file creating this problem is longish and for copyright reasons I can't publish it but based on my description over it shouldn't be hard to create an example. The first problem should appear every time a class produced by a definition contains a property with the same name as the class name (different case for first letter but otherwise similar)

The second problem should pop up whenever attributes of type string and format byte are used

Command line used for generation
    <plugin>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>2.4.14</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
            <configuration>
                   <inputSpec>${project.basedir}/src/main/resources/add-on-api-swagger.yaml</inputSpec>
                    <language>jaxrs-resteasy-eap</language>
                    <configOptions>
                        <sourceFolder>src/main/java</sourceFolder>
                    </configOptions>
                    <modelPackage>com.ec.add.on.api.json</modelPackage>
                    <output>${project.basedir}/target/generated-sources/swagger</output>
                    <generateApis>false</generateApis>
                    <generateApiTests>false</generateApiTests>
                    <generateModelTests>false</generateModelTests>
                    <generateSupportingFiles>false</generateSupportingFiles>
                </configuration>
            </execution>
        </executions>
    </plugin>
Suggest a fix/enhancement

For the first problem prefix own properties with this. For the second problem, use Arrays.equals instead of Objects.equals whenever arrays are compared..

khaksnes commented 3 years ago

Running SpotBugs on the generated code do reveal this kind of errors. See https://spotbugs.github.io/ I was made aware of the problems due to this tool.