OpenAPITools / OpenAPITools/openapi-generator

[BUG] java-inflector templates do not generate proper hashCode

Open
#5,238 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Retrofit Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

JavaInflector/pojo.mustache generates hashCode whican can't be compiled if there are allOf and properties sections

openapi-generator version

org.openapitools:openapi-generator-gradle-plugin:4.2.3

OpenAPI declaration file content or url

Yaml file

openapi: 3.0.1
info:
  title: Test
  version: 1.0.0
servers:
  - url: https://aaa.dd.com
tags:
  - name: Test
    description: Test
paths:
  /test:
    get:
      tags:
        - Test
      summary: test
      parameters:
        - name: fff
          in: query
          description: ffff
          required: true
          allowEmptyValue: true
          schema:
            type: string
      responses:
        405:
          description: invalid
          content: {}
components:
  schemas:            
    Test2:
      description: Standard action object for all items
      required:
        - type
      properties:
        type:
          type: string
          description: this 
          $ref: '#/components/schemas/Test1'
 
    Image:
      description: Standard image object
      allOf:
        - $ref: '#/components/schemas/Test1'
        - type: object
      properties:
        actions:
          type: array
          items:
            $ref: '#/components/schemas/Test2'
    Test1:
      required:
        - cccc
      properties:
        cccc:
          description: test3
          type: string
Command line used for generation
task buildApiSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    generatorName = "java"
    inputSpec = "$rootDir/app/yaml/swagger1.yaml"
    apiPackage = "test.api"
    invokerPackage = "test.invoker"
    modelPackage = "test.model"
    additionalProperties = [
            useRxJava2: "true"
    ]
    outputDir = "$rootDir/apitest".toString()
    configOptions = [
            "java8"         : "true",
            "dateLibrary" : "java8-localdatetime",
            "library"       : "retrofit2",
            "useGzipFeature": "true"
    ]
}
Steps to reproduce

after patch

public class Image extends Test1 {
  public static final String SERIALIZED_NAME_ACTIONS = "actions";
  @SerializedName(SERIALIZED_NAME_ACTIONS)
  private List<Test2> actions = null;


  public Image actions(List<Test2> actions) {
    
    this.actions = actions;
    return this;
  }

  public Image addActionsItem(Test2 actionsItem) {
    if (this.actions == null) {
      this.actions = new ArrayList<>();
    }
    this.actions.add(actionsItem);
    return this;
  }

   /**
   * Get actions
   * @return actions
  **/
  @javax.annotation.Nullable
  @ApiModelProperty(value = "")

  public List<Test2> getActions() {
    return actions;
  }


  public void setActions(List<Test2> actions) {
    this.actions = actions;
  }


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

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


  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Image {\n");
    sb.append("    ").append(toIndentedString(super.toString())).append("\n");
    sb.append("    actions: ").append(toIndentedString(actions)).append("\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    ");
  }

}

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

Should be

public int hashCode() {
return Objects.hash(actions, super.hashCode());
}

Related issues/PRs
Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the JavaInflector/pojo.mustache template and reproduce the generated Image class using the Gradle generation task and the supplied OpenAPI YAML. Check how hashCode is assembled for allOf models with properties. Done means the generated Java compiles and produces Objects.hash(actions, super.hashCode()) rather than concatenating actionssuper.hashCode().

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.