OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Spring] useOptional=true with byte[] field generates wrong equals for the field

Open
#19,680 5 comments 0 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? See https://github.com/vhbcm/openapi-spring-optional-bytes/blob/master/src/main/resources/api.yaml

  • 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?

    The field is private Optional<byte[]> mycontent = Optional.empty(); and the actual output in equals() is

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

    As you can see, the this.mycontent is assumed to be array but it is not. It is Optional<byte[]> instead. The equals() template needs to take this into account.

  • [Optional] Sponsorship to speed up the bug fix or feature request (example)

Description

The generator with useOptional=true option and with format: byte generates Optional<byte[]> field for which it then generates wrong equals() method that does not take the Optional into account.

openapi-generator version

7.8.0 using Maven

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: 'test'
  description: 'Test'
  version: 1.0.0
servers: []
paths:
  '/test':
    get:
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DTO'
components:
  schemas:
    DTO:
      properties:
        mycontent:
          type: string
          format: byte

Generation Details
Steps to reproduce

Checkout the project at https://github.com/vhbcm/openapi-spring-optional-bytes and run mvn clean install.

Related issues/PRs

This looks like similar kind of issue as #17935.

Suggest a fix

🤷🏿

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 by running mvn clean install in the linked reproduction project and inspect the generated DTO's equals() method. Then locate the Spring generator template responsible for model equality and verify the generated result for an Optional<byte[]> field; done means the equality expression handles the Optional-wrapped byte array correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.