Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #13385
Closed
Open
Issue created Sep 08, 2022 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][Java] Generated Java POJOs with JsonNullable do not use equalsNullable in generated equals method

Created by: mariusmanastireanu

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I am trying to generate an object with a property of nullable primitive array type and the code generated does not match my description.

openapi-generator version

5.2.0, 6.0.1, 6.2.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: 'Minimal Example '
  description: byte Array error in equal method
  version: v1
paths:
  /nullable-array-test:
    get:
      summary: ''
      description: ''
      operationId: ''
      parameters: []
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                type: array
                items:
                  "$ref": "#/components/schemas/TestObject"
components:
  schemas:
    TestObject:
      type: object
      properties:
        nullableString:
          type: string
          nullable: true
        picture:
          type: string
          format: byte
          nullable: true
Generation Details

I'm using this in a Spring-Boot project with the following configuration:

             <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.0.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <id>buildApi</id>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/static/openapi.yaml</inputSpec>
                            <generatorName>spring</generatorName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

This generates the following java class:

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-09-26T16:08:14.239092+03:00[Europe/Bucharest]")
public class TestObject {

  @JsonProperty("nullableString")
  private JsonNullable<String> nullableString = JsonNullable.undefined();

  @JsonProperty("picture")
  private JsonNullable<byte[]> picture = JsonNullable.undefined();

  public TestObject nullableString(String nullableString) {
    this.nullableString = JsonNullable.of(nullableString);
    return this;
  }

  /**
   * Get nullableString
   * @return nullableString
  */
  
  @Schema(name = "nullableString", required = false)
  public JsonNullable<String> getNullableString() {
    return nullableString;
  }

  public void setNullableString(JsonNullable<String> nullableString) {
    this.nullableString = nullableString;
  }

  public TestObject picture(byte[] picture) {
    this.picture = JsonNullable.of(picture);
    return this;
  }

  /**
   * Get picture
   * @return picture
  */
  
  @Schema(name = "picture", required = false)
  public JsonNullable<byte[]> getPicture() {
    return picture;
  }

  public void setPicture(JsonNullable<byte[]> picture) {
    this.picture = picture;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    TestObject testObject = (TestObject) o;
    return Objects.equals(this.nullableString, testObject.nullableString) &&
        Arrays.equals(this.picture, testObject.picture);
  }

  private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b) {
    return a == b || (a != null && b != null && a.isPresent() && b.isPresent() && Objects.deepEquals(a.get(), b.get()));
  }

  @Override
  public int hashCode() {
    return Objects.hash(nullableString, Arrays.hashCode(picture));
  }

  private static <T> int hashCodeNullable(JsonNullable<T> a) {
    if (a == null) {
      return 1;
    }
    return a.isPresent() ? Arrays.deepHashCode(new Object[]{a.get()}) : 31;
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class TestObject {\n");
    sb.append("    nullableString: ").append(toIndentedString(nullableString)).append("\n");
    sb.append("    picture: ").append(toIndentedString(picture)).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(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}

As you can see the equalsNullable method is not being used, instead it is being used the Arrays.equals(). Similarly Objects.equals() is being used instead of equalsNullable for other properties I try to add.

Related issues/PRs

I've done some searches and seems that the issue was fixed with this commit: https://github.com/OpenAPITools/openapi-generator/pull/10012 However, I tried also generating the same output with version 5.2.0 - which seems to be the one that contains the fix and I have the same results. Something is clearly missing/faulty.

Edit: Gave more details and a simplified example.

Assignee
Assign to
Time tracking