OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JavaJaxRS] Using `generateBuilders=true` breaks generated models

Open
#11,479 0 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

Switching generateBuilders on, for an example Spec like follows:

      "ExampleWrapper": {
        "type": "object",
        "properties": {
          "street": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }

leads to code lke this:

public class ExampleWrapper {
[...]
  private @Valid String street;

  public ExampleWrapper(){
  }

  private @Valid String name;

  public ExampleWrapper(){
  }

[...methods...]
[...builder definition...]

As one can see, the same no-arg-constructor is created over and over, leading to compile errors.

openapi-generator version

5.4.0 (should also appear in older 5.x versions, haven't tracked any further)

OpenAPI declaration file content or url
Generation Details

openapi-generator-maven-plugin:5.4.0
with config:

                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/META-INF/openapi.json</inputSpec>
                            <configurationFile>${project.basedir}/src/main/openapi/generator/configuration.json</configurationFile>
                            <apiPackage>com.example.api.generated</apiPackage>
                            <modelPackage>com.example.api.generated.model</modelPackage>
                            <generatorName>jaxrs-spec</generatorName>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <returnResponse>false</returnResponse>
                                <sourceFolder>java</sourceFolder>
                                <useSwaggerAnnotations>false</useSwaggerAnnotations>
                                <generatePom>false</generatePom>
                                <dateLibrary>java8</dateLibrary>
                                <generateBuilders>true</generateBuilders>
                            </configOptions>
                        </configuration>
Steps to reproduce

see above

Related issues/PRs
Suggest a fix

moving the section...

  {{#generateBuilders}}public {{classname}}({{#vars}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/vars}}) {
    {{#vars}}
    this.{{name}} = {{name}};
    {{/vars}}
  }{{/generateBuilders}}

...from here: https://github.com/OpenAPITools/openapi-generator/blob/v5.4.0/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache#L39-L43

...up a few lines, to be outside of the other {{#vars}}-loop. How about here: https://github.com/OpenAPITools/openapi-generator/blob/v5.4.0/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache#L22

I am puzzled that is ever worked, given the inital commit https://github.com/OpenAPITools/openapi-generator/commit/62c346f8da5a8f643396b48e8bfe5d0bc482793c already created constructors inside the "property rendering loop", so for n properties, it should've rendered n identical constructors!?

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 generateBuilders constructor block and its surrounding vars loop. Reproduce with the supplied JavaJaxRS Maven configuration and confirm that a model with multiple properties generates only one no-argument constructor and compiles without duplicate-constructor errors.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.