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] [Spring] useOptional=true with byte[] field generates wrong equals for the field #19680

Open
4 of 6 tasks
vhbcm opened this issue Sep 25, 2024 · 3 comments
Open
4 of 6 tasks

Comments

@vhbcm
Copy link

vhbcm commented Sep 25, 2024

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

🤷🏿

@rangarajl
Copy link

rangarajl commented Sep 26, 2024

The code generation in this case is fundamentally based on the JSON Schema specifications, which specifies to semantically annotate format as per the reference here: https://json-schema.org/draft/2020-12/json-schema-validation#section-8

Regarding this specific issue, binary data in JSON should be encoded as a base64 string. The schema isn't used to validate the payload directly, as documents containing binary data may include control characters that invalidate the JSON document. This encoding ensures proper handling of binary data within the JSON structure.
A similar issue is highlighted here - mozilla/jsonschema-transpiler#81

So not sure if format bytes is valid in the first place, please add any context which I might have missed here

@vhbcm
Copy link
Author

vhbcm commented Sep 26, 2024

The type: string with format: byte is also included in the spec. See the table in 4.4 Data Types: https://spec.openapis.org/oas/v3.0.3#data-types

@vhbcm
Copy link
Author

vhbcm commented Sep 26, 2024

image

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

2 participants