OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.42k stars 6.48k forks source link

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

Open asjp1970 opened 3 months ago

asjp1970 commented 3 months ago

Bug Report Checklist

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
jpfinne commented 3 months ago

{{^-last}}, {{/-last}} is the standard way of mustache.

You can adapt your pojo.mustache by looking at the default java of spring templates

asjp1970 commented 3 months ago

Thanks @jpfinne , that solved the issue. But I still think there is a bug in the Java template engine, since it is something that worked before and does not seem to be deprecated.