Skip to content

[Java][Spring] Do not generate required args constructor that duplicates @lombok.AllArgsConstructor - #25396

Open
fixfon wants to merge 2 commits into
OpenAPITools:masterfrom
fixfon:fix/25395-spring-lombok-allargs-duplicate-ctor
Open

fixfon wants to merge 2 commits into
OpenAPITools:masterfrom
fixfon:fix/25395-spring-lombok-allargs-duplicate-ctor

Conversation

@fixfon

@fixfon fixfon commented Oct 24, 2026

Copy link
Copy Markdown

Description:

With the spring or java-camel generator and additionalProperties.lombok.AllArgsConstructor=true, a model whose properties are all required got two constructors with the same signature: the "Constructor with only required parameters" the generator writes, and the one Lombok adds for @lombok.AllArgsConstructor. The generated class did not compile (constructor ... is already defined).

In SpringCodegen.postProcessAllModels I now set a vendor extension on a model when @lombok.AllArgsConstructor is on and the generated required args constructor would have Lombok's signature. JavaSpring/pojo.mustache and java-camel-server/pojo.mustache skip that constructor when the extension is set. The no-args constructor stays.

The extension is only set when every own property is required and requiredVars.size() == vars.size(). requiredVars also holds inherited properties, so a model with a required parent property (Dog(String breed, String kind) vs. Lombok's Dog(String breed)) keeps its constructor. A nullable property with openApiNullable=true is a JsonNullable<T> field but a T parameter, so those models keep it too.

I added three tests in SpringCodegenTest plus 3_0/spring/issue_25395.yaml: the all-required case, the nullable case with openApiNullable=true, and the inherited case. The first and third fail without the change and pass with it. The nullable test passes either way and guards the case that must not change. samples/ has no diff.

Fixes #25395

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples (see below for what I ran instead):
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

What I ran on this branch (macOS, Maven wrapper, offline after the first dependency download):

  • ./mvnw -B -o -pl modules/openapi-generator -am test -Dtest='SpringCodegenTest#lombokAllArgs*' passes (3 tests). With the three source files put back to master, the all-required and inherited tests fail.
  • ./mvnw -B -o -pl modules/openapi-generator -am test -Dtest=SpringCodegenTest,SpringPageableScanUtilsTest,SpringHttpStatusLambdaTest,JavaValidationArrayPrimitivesTest passes (632 tests, 0 failures).
  • I generated the spring and java-camel output for the fixture spec with and without the change and compiled it with javac and Lombok. Master fails with constructor Pet(Long,String) is already defined (and the same for Animal and NicknamedPet). With the fix it compiles, also with openApiNullable=true.
  • ./bin/generate-samples.sh ./bin/configs/spring-boot-lombok-data.yaml exits 0 and leaves samples/ unchanged. It is the only config that mentions AllArgsConstructor.

I did not run the full ./mvnw clean package, the other sample configs, or ./bin/utils/export_docs_generators.sh, so the second box is unchecked. No option or docs text changed.

cc @cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @banlevente @Zomzog @martin-mfg @KannaKim

…tes @lombok.AllArgsConstructor

When every property of a model is required, the generated required args constructor has the same signature as the one Lombok generates for @lombok.AllArgsConstructor, so the generated class does not compile. Skip the generated constructor in that case and keep it when the signatures differ (nullable properties with openApiNullable, parent properties).

Fixes OpenAPITools#25395

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 5 files

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

…es adds a Lombok parameter

The generated additionalProperties map is a field too, so @lombok.AllArgsConstructor takes it as an extra parameter and no longer collides with the required args constructor.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][JAVA][Spring] Duplicate constructor when using @lombok.AllArgsConstructor and all properties are required

1 participant