Skip to content

[BUG][Java][Spring] Compilation error for string fields with byte format that are marked nullable and required #17935

@AntarEspadas

Description

@AntarEspadas

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

Having a field of type string with format byte marked as both nullable and required will result in the generator creating a model with a field of type JsonNullable<byte[]> (So far, so good). However, in the implementation of the equals method, the generated code will try to compare the values of this field using Arrays.equals, resulting in a compilation error.

openapi-generator version

7.4.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Nullable string<byte> bug
  version: 1.0.0
paths: {}
components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          type: string
          format: byte
          nullable: true
      required:
        - bar
Generation Details

spring generator.
Field string with format byte marked as nullable and required

Output
  // ...
  
 private JsonNullable<byte[]> bar = JsonNullable.<byte[]>undefined();
 
 // ...

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Foo foo = (Foo) o;
    return Arrays.equals(this.bar, foo.bar);
  }

  // ...
Steps to reproduce
  1. Use above specification with filename api-docs.yml
  2. Run the generator docker run --rm -v "$PWD:/local" openapitools/openapi-generator-cli:latest generate -i /local/api-docs.yml -g spring -o /local/output
  3. Change to generated folder cd output
  4. Attempt to compile project mvn compile
Related issues/PRs

Couldn't find any

Suggest a fix

When the field is not marked required, the generated code is correct.

  // ...

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Foo foo = (Foo) o;
    return equalsNullable(this.bar, foo.bar);
  }

  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()));
  }

  // ...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions