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] [JAX-RS] Builder generates multiple constructors, and fails to compile #8132

Open
5 of 6 tasks
danielhodder opened this issue Dec 8, 2020 · 0 comments · May be fixed by #8133
Open
5 of 6 tasks

[BUG] [JAX-RS] Builder generates multiple constructors, and fails to compile #8132

danielhodder opened this issue Dec 8, 2020 · 0 comments · May be fixed by #8133

Comments

@danielhodder
Copy link

danielhodder commented Dec 8, 2020

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

When using the JAX-RS Spec generator you are able to ask the builder to create a builder for model that will be transferred. This was added in #4930. However, the way it was implemented means that the constructor of the object is repeated for each field in the model.

Essentially the template for the constructor (https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache#L37-L41) is inside the loop which is generating the variables. This results in java code like this:

  private @Valid Long id;
  private @Valid String name = "default-name";

  /**
   **/
  public Category id(Long id) {
    this.id = id;
    return this;
  }

  public Category() {
  }

  
  @ApiModelProperty(value = "")
  @JsonProperty("id")
  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }/**
   **/
  public Category name(String name) {
    this.name = name;
    return this;
  }

  public Category() {
  }

  
  @ApiModelProperty(required = true, value = "")
  @JsonProperty("name")
  @NotNull
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

The issue here is that the same constructor is here twice, and it doesn't have any parameters. I believe this latter is because we're inside a loop and the nesting doesn't work. The result of this is that the generated java code does not compile:

[ERROR] /home/danielho/Projects/OpenSource/openapi-generator/samples/server/petstore/jaxrs-spec-interface/src/gen/java/org/openapitools/model/MapTest.java:[86,10] constructor MapTest() is already defined in class org.openapitools.model.MapTest
[ERROR] /home/danielho/Projects/OpenSource/openapi-generator/samples/server/petstore/jaxrs-spec-interface/src/gen/java/org/openapitools/model/MapTest.java:[105,10] constructor MapTest() is already defined in class org.openapitools.model.MapTest
[ERROR] /home/danielho/Projects/OpenSource/openapi-generator/samples/server/petstore/jaxrs-spec-interface/src/gen/java/org/openapitools/model/MapTest.java:[124,10] constructor MapTest() is already defined in class org.openapitools.model.MapTest
[ERROR] /home/danielho/Projects/OpenSource/openapi-generator/samples/server/petstore/jaxrs-spec-interface/src/gen/java/org/openapitools/model/Order.java:[86,10] constructor Order() is already defined in class org.openapitools.model.Order
[ERROR] /home/danielho/Projects/OpenSource/openapi-generator/samples/server/petstore/jaxrs-spec-interface/src/gen/java/org/openapitools/model/Order.java:[105,10] constructor Order() is already defined in class org.openapitools.model.Order
[ERROR] /home/danielho/Projects/OpenSource/openapi-generator/samples/server/petstore/jaxrs-spec-interface/src/gen/java/org/openapitools/model/Order.java:[124,10] constructor Order() is already defined in class org.openapitools.model.Order
[ERROR] /home/danielho/Projects/OpenSource/openapi-generator/samples/server/petstore/jaxrs-spec-interface/src/gen/java/org/openapitools/model/Order.java:[144,10] constructor Order() is already defined in class org.openapitools.model.Order
[ERROR] /home/danielho/Projects/OpenSource/openapi-generator/samples/server/petstore/jaxrs-spec-interface/src/gen/java/org/openapitools/model/Order.java:[163,10] constructor Order() is already defined in class org.openapitools.model.Order
[ERROR] /home/danielho/Projects/OpenSource/openapi-generator/samples/server/petstore/jaxrs-spec-interface/src/gen/java/org/openapitools/model/Capitalization.java:[52,10] constructor Capitalization() is already defined in class org.openapitools.model.Capitalization

I believe the constructor generator only needs to be moved to get this to work. It's still not possible to generate builders for types with parents, but that was already the case in #4930.

openapi-generator version

I have tested this occours in the latest master branch, and on the last release version 4.3.1

OpenAPI declaration file content or url

This was generated from the petstore sample in the repostitory

Generation Details

Add the following to the jaxrs-spec-interface.yaml sample generation file:

generateBuilders: "true"
Steps to reproduce
  1. Add generateBuilders: "true" to the jaxrs-spec-interface sample generation
  2. Generate the samples.
  3. Try to compile the petstore sample

This will fail as above

Related issues/PRs

This seems to have always been an issue since it was introduced in #4930

Suggest a fix

I believe this can be fixed by moving the constructor generation further down the template, outside the vars loop. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache#L37-L41. I will make a PR for this change.

danielhodder added a commit to danielhodder/openapi-generator that referenced this issue Dec 8, 2020
Using the 'generateBuilders' no longer generates multiple constructors,
but only one for each class. This still does not work with models that
use inheritance.

Fixes OpenAPITools#8132
@danielhodder danielhodder linked a pull request Dec 8, 2020 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant