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][Spring] Compilation error for string fields with byte format that are marked nullable and required #17935

Open
5 of 6 tasks
AntarEspadas opened this issue Feb 23, 2024 · 0 comments

Comments

@AntarEspadas
Copy link

AntarEspadas commented Feb 23, 2024

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

  // ...
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

1 participant