Repository navigation
[Java][Spring] Do not generate required args constructor that duplicates @lombok.AllArgsConstructor - #25396
Open
fixfon wants to merge 2 commits into
Open
[Java][Spring] Do not generate required args constructor that duplicates @lombok.AllArgsConstructor#25396fixfon wants to merge 2 commits into
fixfon wants to merge 2 commits into
Conversation
…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
Contributor
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
With the
springorjava-camelgenerator andadditionalProperties.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.postProcessAllModelsI now set a vendor extension on a model when@lombok.AllArgsConstructoris on and the generated required args constructor would have Lombok's signature.JavaSpring/pojo.mustacheandjava-camel-server/pojo.mustacheskip 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().requiredVarsalso holds inherited properties, so a model with a required parent property (Dog(String breed, String kind)vs. Lombok'sDog(String breed)) keeps its constructor. A nullable property withopenApiNullable=trueis aJsonNullable<T>field but aTparameter, so those models keep it too.I added three tests in
SpringCodegenTestplus3_0/spring/issue_25395.yaml: the all-required case, the nullable case withopenApiNullable=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
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,JavaValidationArrayPrimitivesTestpasses (632 tests, 0 failures).springandjava-cameloutput for the fixture spec with and without the change and compiled it withjavacand Lombok. Master fails withconstructor Pet(Long,String) is already defined(and the same forAnimalandNicknamedPet). With the fix it compiles, also withopenApiNullable=true../bin/generate-samples.sh ./bin/configs/spring-boot-lombok-data.yamlexits 0 and leavessamples/unchanged. It is the only config that mentionsAllArgsConstructor.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