[BUG] [JAX-RS] Builder generates multiple constructors, and fails to compile
Created by: danielhodder
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
- Add
generateBuilders: "true"
to the jaxrs-spec-interface sample generation - Generate the samples.
- 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.