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

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

Closed
5 of 6 tasks
mariusmanastireanu opened this issue Sep 8, 2022 · 1 comment

Comments

@mariusmanastireanu
Copy link

mariusmanastireanu commented Sep 8, 2022

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: #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.

@mariusmanastireanu mariusmanastireanu changed the title [BUG][Java] JsonNullable with primitive array type results in compilation error in equals() method [BUG][Java] Generated Java POJOs with JsonNullable do not use equalsNullable in generated equals method Sep 26, 2022
@borsch
Copy link
Member

borsch commented Sep 27, 2022

Hi @mariusmanastireanu
will look over weekend

@wing328 wing328 closed this as completed in 10a1e7c Oct 6, 2022
jayandran-Sampath pushed a commit to jayandran-Sampath/openapi-generator that referenced this issue Oct 6, 2022
* Fixing to use equalsNullable when nullable set in config for SpringCodeGen

* Adding additional test case file

* removed print statement from SpringCodeGen

* Updated model object

* Corrected indentation and removed import

* Fixed broken test

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

No branches or pull requests

2 participants