OpenAPITools / OpenAPITools/openapi-generator

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

Open
#17,935 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

  // ...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided api-docs.yml and run the spring generator using the documented Docker command. Inspect the generated Foo model and compile the output with mvn compile. Done means the nullable, required byte-format field produces generated Java that compiles and its equals behavior is covered consistently with the non-required case.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.