OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.73k stars 6.56k forks source link

[BUG] java-inflector templates do not generate proper hashCode and equals functions #3832

Open jasonculverhouse opened 5 years ago

jasonculverhouse commented 5 years ago

Bug Report Checklist

Description

JavaInflector/pojo.mustache does not generate proper hashCode and equals functions

openapi-generator version

latest

OpenAPI declaration file content or url

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/allOf.yaml

openapi: 3.0.1
info:
  version: 1.0.0
  title: Example
  license:
    name: MIT
servers:
  - url: http://api.example.xyz/v1
paths:
  /person/display/{personId}:
    get:
      parameters:
        - name: personId
          in: path
          required: true
          description: The id of the person to retrieve
          schema:
            type: string
      operationId: list
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Person"
components:
  schemas:
    Person:
      type: object
      discriminator:
        propertyName: $_type
        mapping:
          a: '#/components/schemas/Adult'
          c: Child
      properties:
        $_type:
          type: string
        lastName:
          type: string
        firstName:
          type: string
    Adult:
      description: A representation of an adult
      allOf:
      - $ref: '#/components/schemas/Person'
      - type: object
        properties:
          children:
            type: array
            items:
              $ref: "#/components/schemas/Child"
    Child:
      description: A representation of a child
      allOf:
      - type: object
        properties:
          age:
            type: integer
            format: int32
      - $ref: '#/components/schemas/Person'
Command line used for generation
docker run --rm -v /local/openapi:/local openapitools/openapi-generator-cli generate -o /local/out -i /local/allOf.yaml -g java-inflector
Steps to reproduce

in the Generated Classes you can see that the super class is not considered in equals or hash code

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

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

After the patch below

  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Adult adult = (Adult) o;
    return Objects.equals(this.children, adult.children) &&
      super.equals(o);
  }

  @Override
  public int hashCode() {
    return Objects.hash(children, super.hashCode());
  }
Related issues/PRs
Suggest a fix
diff --git a/modules/openapi-generator/src/main/resources/JavaInflector/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaInflector/pojo.mustache
index badfdbc35a..f824eb3af9 100644
--- a/modules/openapi-generator/src/main/resources/JavaInflector/pojo.mustache
+++ b/modules/openapi-generator/src/main/resources/JavaInflector/pojo.mustache
@@ -48,7 +48,6 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
   }

   {{/vars}}
-
   @Override
   public boolean equals(java.lang.Object o) {
     if (this == o) {
@@ -57,15 +56,17 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
     if (o == null || getClass() != o.getClass()) {
       return false;
     }
-    {{classname}} {{classVarName}} = ({{classname}}) o;{{#hasVars}}
-    return {{#vars}}Objects.equals({{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
-        {{/hasMore}}{{^hasMore}};{{/hasMore}}{{/vars}}{{/hasVars}}{{^hasVars}}
+    {{#hasVars}}
+    {{classname}} {{classVarName}} = ({{classname}}) o;
+    return {{#vars}}Objects.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} &&
+      {{/hasMore}}{{/vars}}{{#parent}} &&
+      super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
     return true;{{/hasVars}}
   }

   @Override
   public int hashCode() {
-    return Objects.hash({{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}});
+    return Objects.hash({{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}});
   }

   @Override
auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

wing328 commented 5 years ago

@jasonculverhouse can you please submit a PR with the suggested fix?

TheOneAndOnlyAndreKempe commented 3 years ago

Hej everyone,

we are working on the 4.3.1 release of the OpenAPIGenerator. Today we encountered the very same problem that the TO described and fixed. It seems like the changes didn't make into the release of OpenAPIGenerator.

Does anyone have an idea if and in which release the described bug will be fixed?

Cheers + Best regards, André

jasonculverhouse commented 3 years ago

@wing328 @TheOneAndOnlyAndreKempe I made this pull request https://github.com/OpenAPITools/openapi-generator/pull/7956. Where I just sync the hashcode and equals with the JavaSpring project.

I don't think that the java code has been regenerated for the inflector project since ~3.x

I haven't succeeded in getting the code to run correctly...