OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Java] hasMore is not working as expected with Java using version 7.6.0 of openapi-generator-maven-plugin

Open
#18,866 2 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?
  • 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

I came from version 4.0.2 of openapi-generator-maven-plugin and uplifted it to version 7.6.0. We are using our own templates to produce only the pojo's of our model. In our template pojo.mustache we need to generate equals and hash methods, so we need to iterate over {{vars}} until the last one to stop adding "&&" for the AND operation in equals method, or "," for adding object's attributes to the hash method.

@Override
  public int hashCode() {
  return 0{{^isString}}{{^isInteger}}
      + java.util.Objects.hash({{^hasVars}}{{#parent}},{{/parent}}{{/hasVars}}
      {{! Here we are using hasMore directive to conditionally add a "," after every attribute of the
       object to add them to the hash function. In some version after 4.0.2 of the
       openapi-generator-maven-plugin does not work any longer (hasMore is undefined) }}
      {{#vars}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{#hasMore}}, {{/hasMore}}{{/vars}}
      {{#parent}}{{#hasVars}}, {{/hasVars}}{{classVarName}}{{/parent}}){{/isInteger}}{{/isString}};
  }

Here is the complete pojo.template in github.

In 4.4.2 this was working perfectly and this was the expected result (for the hash):

  @Override
  public int hashCode() {
  return 0
      + java.util.Objects.hash(
      type, title, authors, pages
      );
  }

But, when we use version 7.6.0, this is the generated code, which, of course breaks the compilation because symbol typetitleauthorspages does not exist:

  @Override
  public int hashCode() {
  return 0
      + java.util.Objects.hash(
      typetitleauthorspages
      );
  }

As you can see, as hasMore is, for some reason, left undefined, the "," separating each one of the object's fields is not added and all attributes names are concanetated.

Here is a minimal version of the project in github just to showcase and reproduce the problem with the minimum setup: https://github.com/asjp1970/openapi

openapi-generator version

7.6.0

But we have tried with previous versions between 4.0.2 and 7.6.0 with the same result. Something must have happened with the code generator when processing the variables of the templates.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Sample API
  version: 1.0.0

paths:
  /book:
    get:
      summary: Get an item
      operationId: getItem
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Book'

components:
  schemas:
    Book:
      type: object
      required:
        - type
        - title
        - authors
      properties:
        type:
          $ref: '#/components/schemas/ItemType'
        title:
          type: string
        authors:
          type: array
          items:
            type: string
        pages:
          type: integer
          format: int32

    Movie:
      type: object
      required:
        - type
        - title
        - director
      properties:
        type:
          $ref: '#/components/schemas/ItemType'
        title:
          type: string
        director:
          type: string
        duration:
          type: integer
          format: int32
          description: Duration in minutes

    ItemType:
      type: string
      enum:
        - BOOK
        - MOVIE

Here is the api_sample.yaml in github.

Generation Details

Recreating the problem using the project in github is quite simple, no need to use CLI. The versions in github is ready to reproduce the problem.

Steps to reproduce
  1. clone the repo:
     $ git clone git@github.com:asjp1970/openapi.git
    
  2. compile:
     $ mvn clean compile
    
  3. then go to target/generated-sources/openapi/src/main/java/com/softvarivm/openapi/model/Book to see the compilation failure:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.068 s
[INFO] Finished at: 2024-06-05T08:13:07Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project openapi-tests: Compilation failure: Compilation failure: 
[ERROR] /usr/src/app/target/generated-sources/openapi/src/main/java/com/softvarivm/openapi/model/Book.java:[146,7] cannot find symbol
[ERROR]   symbol:   variable typetitleauthorspages
[ERROR]   location: class com.softvarivm.openapi.model.Book
[ERROR] /usr/src/app/target/generated-sources/openapi/src/main/java/com/softvarivm/openapi/model/Movie.java:[144,7] cannot find symbol
[ERROR]   symbol:   variable typetitledirectorduration
[ERROR]   location: class com.softvarivm.openapi.model.Movie
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[~/my_github_repos/openapi]
  1. Optionally, if you want to check how dit it work before, edit line 78 in pom.xml and replace 7.6.0 with 4.0.2 to see that everything worked like a charm.
Related issues/PRs

No similar issues fouund.

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 with the linked repository's pom.xml around line 78 and src/main/resources/models/pojo.mustache, then reproduce the problem with mvn clean compile. Inspect the generated Book.java and Movie.java under target/generated-sources/openapi/src/main/java/com/softvarivm/openapi/model/. Done means the generated Java models compile and separators appear between fields as shown in the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.