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

[spring] equals and hashCode for models with array type [] #1448

Closed
canaz opened this issue Nov 14, 2018 · 5 comments · Fixed by #8345
Closed

[spring] equals and hashCode for models with array type [] #1448

canaz opened this issue Nov 14, 2018 · 5 comments · Fixed by #8345

Comments

@canaz
Copy link

canaz commented Nov 14, 2018

Description

equals() and hashCode() are not generated correctly for generatorName spring and library spring-boot.
It uses Objects.equals() to compare primitive arrays.

The same model generated with generatorName java and library resttemplate uses Arrays.equals(), which ist correct.

openapi-generator version

3.3.2

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: "1"
  title: "Test Api"

servers:
  - url: "localhost:8282"

paths:

  /test:
    post:
      operationId: "doTest"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Test'

      responses:
        200:
          description: "Ok"
        400:
          description: "Bad Request"
        500:
          description: "Internal Server Error"


components:
  schemas:

    Test:
      type: object
      required:
        - name
        - stream
      properties:
        name:
          type: string
        stream:
          description: "byte array with binary data"
          type: string
          format: byte
Command line used for generation

Used pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>my-test</artifactId>
    <version>1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>${project.artifactId}</name>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.5.FINAL</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.4</version>
        </dependency>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.7</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <!-- Swagger config START -->
            <plugin>
                <!-- fuer das Generieren aus dem swagger....yaml file-->
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>3.3.2</version>
                <dependencies>
                    <dependency>
                        <groupId>org.openapitools</groupId>
                        <artifactId>openapi-generator-maven-plugin</artifactId>
                        <version>3.3.2</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>buildSwagger</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>src/main/resources/swagger.yaml</inputSpec>

                            <generatorName>spring</generatorName>
                            <library>spring-boot</library>

                            <generateApis>true</generateApis>
                            <generateApiTests>false</generateApiTests>
                            <generateApiDocumentation>false</generateApiDocumentation>
                            <generateModels>true</generateModels>
                            <generateModelTests>false</generateModelTests>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <generateSupportingFiles>true</generateSupportingFiles>

                            <environmentVariables>
                                <supportingFiles>
                                    ApiClient.java,ApiException.java,ApiOriginFilter.java,ApiResponseMessage.java,HomeController.java,NotFoundException.java,SwaggerDocumentationConfig.java,RFC3339DateFormat.java,OAuth.java,ApiKeyAuth.java,HttpBasicAuth.java,Authentication.java
                                </supportingFiles>
                            </environmentVariables>

                            <configOptions>
                                <dateLibrary>java8</dateLibrary>
                                <useBeanValidation>true</useBeanValidation>
                                <performBeanValidation>true</performBeanValidation>
                                <modelPackage>com.test.model</modelPackage>
                                <apiPackage>com.test.api</apiPackage>
                                <sourceFolder>/src/main/java</sourceFolder>
                                <implFolder>/src/main/java</implFolder>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
Steps to reproduce

Create project with given pom file and put swagger.yaml to src/main/resources

@wing328
Copy link
Member

wing328 commented Nov 20, 2018

The same model generated with generatorName java and library resttemplate uses Arrays.equals(), which ist correct.

Do you mean using Arrays.equals() is correct?

@canaz
Copy link
Author

canaz commented Nov 22, 2018

Yes.
array1.equals(array2) will be true only if they are the same instances (referencing same object)

Arrays.equals(array1, array2) will be true if each elements are equal (element1.equals(element2) is true) at same indexes. This method will not compare the nested arrays' elements.

Arrays.deepEquals(array1, array2): same as Array.equals() but will also check nested arrays recursively.

Also use the Arrays.hashCode() method to calculate the hashcode

@canaz canaz changed the title [spring] equals and hashCode for models with byte[] [spring] equals and hashCode for models with array type [] Nov 22, 2018
@wing328
Copy link
Member

wing328 commented Nov 22, 2018

The same model generated with generatorName java and library resttemplate uses Arrays.equals(), which ist correct.

Thanks for the clarification. Do you mind filing a PR by copying the fix from resttempalte template?

@canaz
Copy link
Author

canaz commented Nov 22, 2018

I am not allowed to open a new branch, but in fact it will be the same change as https://github.com/swagger-api/swagger-codegen/pull/7341/files/450e72180522c0ad09ae6e28e277dabc348cae78
but for the JavaSpring model

@wing328
Copy link
Member

wing328 commented Dec 30, 2018

@canaz to contribute, please fork this repo to start with. I'm reachable via https://gitter.im if you need help on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants