OpenAPITools / OpenAPITools/openapi-generator

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

Open
#8,132 0 comments 7 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

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.

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 modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache, especially the constructor section and the vars loop. Enable generateBuilders in the jaxrs-spec-interface.yaml petstore sample, regenerate the samples, and compile them. Done means the generated models compile without duplicate no-argument constructors.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.