Skip to content

[BUG][JAVA] Error when generating nullable byte array #9996

Closed
@Polly-Schulze

Description

@Polly-Schulze

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

When there is a nullable byte array in the json file:

          "SomeByteArray": {
            "type": "object",
            "description": "something",
            "additionalProperties": false,
            "properties": {
              "someField": {
                "type": "string",
                "description": "byte array.",
                "format": "byte",
                "nullable": true
              }
            }
          }

Then the file generated looks like this:

@ApiModel(description = "something")
@JsonPropertyOrder({
  SomeByteArray.JSON_PROPERTY_SOME_FIELD
})
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2021-07-21T10:10:33.395362+02:00[Europe/Berlin]")
public class SomeByteArray {
  public static final String JSON_PROPERTY_SOME_FIELD = "someField";
  private JsonNullable<byte[]> someField = JsonNullable.<byte[]>undefined();

// GETTER AND SETTER

  /**
   * Return true if this SomeByteArray object is equal to o.
   */
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    SomeByteArray someByteArray = (SomeByteArray) o;
    return Arrays.equals(this.someField, someByteArray.someField);
  }

// HASH CODE etc.
}

This results in an compiler error, since Arrays.equals(this.someField, someByteArray.someField); compares Arrays, but someField is a JsonNullable of an Array.
In an earlier version the line was generated to: Objects.equals(this.someField, someByteArray.someField);, which does not result in an error.

openapi-generator version

In this version:
java -jar ~/Downloads/openapi-generator-cli-4.3.1.jar generate -g spring --additional-properties=library=spring-mvc -o minimalExample -i src/main/resources/minimalExample.json
the error does not occur as there are still Objects compared, not Arrays.

In the newest version of different libraries the error occurs:
java -jar ~/Downloads/openapi-generator-cli-5.2.0.jar generate -g spring --additional-properties=library=spring-mvc -o minimalExample-2 -i src/main/resources/minimalExample.json

java -jar ~/Downloads/openapi-generator-cli-5.2.0.jar generate -g java --additional-properties=library=webclient -o minimalExample-3 -i src/main/resources/minimalExample.json

java -jar ~/Downloads/openapi-generator-cli-5.2.0.jar generate -g java --additional-properties=library=jersex2 -o minimalExample-4 -i src/main/resources/minimalExample.json

Java version:

 ~ java -version
openjdk version "16.0.1" 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-16.0.1+9 (build 16.0.1+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-16.0.1+9 (build 16.0.1+9, mixed mode, sharing)
OpenAPI declaration file content or url
{
  "x-generator": "NSwag v13.11.3.0 (NJsonSchema v10.4.4.0 (Newtonsoft.Json v13.0.0.0))",
  "openapi": "3.0.0",
  "info": {
    "title": "Minimal Example ",
    "description": "byte Array error in equal method",
    "version": "v1"
  },
  "paths": {
    "/some/path/": {
      "get": {
        "summary": "",
        "description": "",
        "operationId": "",
        "parameters": [],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/SomeByteArray"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
      "components": {
        "schemas": {
          "SomeByteArray": {
            "type": "object",
            "description": "something",
            "additionalProperties": false,
            "properties": {
              "someField": {
                "type": "string",
                "description": "byte array.",
                "format": "byte",
                "nullable": true
              }
            }
          }
        }
      }
}
Steps to reproduce
  1. Save the code above as json
  2. Execute one of the above (see part about version)
  3. Check the file SomeByteArray.java

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions