Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] implement #18032 add builder pattern to java client and spring generator #18650

Merged
merged 3 commits into from
May 14, 2024

Conversation

wing328
Copy link
Member

@wing328 wing328 commented May 13, 2024

based on #18648

Fix #18032. Add a new configOptions: generateBuilders to java client and spring generators

The name of the option is the same as in JavaJAXSRSSpecServerCodeGen.

Spring + lombok: generateBuilders + generateConstructorWithAllArgs configOptions can be used instead of lombok.

The builder is not created when lombok.Data is configured (issue with fluent setters not generated)

Improve generation of no args constructor.
Also minor fix for the generateConstructorWithAllArgs and Lombok.AllArgsConstructor (there was some typos)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)
Java Spring | @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)

@wing328 wing328 changed the title Jpfinne feat add java builder pattern [java] implement #18032 add builder pattern to java client and spring generator May 13, 2024
@wing328 wing328 added this to the 7.6.0 milestone May 13, 2024
@wing328 wing328 marked this pull request as ready for review May 14, 2024 03:32
@wing328
Copy link
Member Author

wing328 commented May 14, 2024

thanks for the PR. let's give it a try

@wing328 wing328 merged commit 51ef868 into master May 14, 2024
204 checks passed
@wing328 wing328 deleted the jpfinne-featAddJavaBuilderPattern branch May 14, 2024 03:38
renatomameli pushed a commit to renatomameli/openapi-generator that referenced this pull request May 17, 2024
…t and spring generator (OpenAPITools#18650)

* add builder pattern to java client and spring generator

* regenerate samples

* update doc

---------

Co-authored-by: jpfinne <jeanpaul@finne.be>
@jpfinne
Copy link
Contributor

jpfinne commented May 21, 2024

@wing328 The builder option is missing from the 7.6.0 release notes.
Is that on purpose because it is not production ready?

@wing328
Copy link
Member Author

wing328 commented May 21, 2024

just updated it to highlight this change as well.

(the release notes are only the highlight, not the full list of changes)

@kevin1chun
Copy link

@wing328 is this available in java-webclient as well?

@jpfinne
Copy link
Contributor

jpfinne commented Jun 12, 2024

@kevin1chun I'm the main contributor of this feature.
I've not done tests with java-webclient. Nevertheless, the WebClient generator shares the same JavaCodeGen and pojo.mustache as the Resttemplate generator. So I would say yes, this is available for you.

Feedbacks are welcome

@kevin1chun
Copy link

Thanks @jpfinne
I've successfully integrated the builders into my java-webclient codegen.
It is much cleaner now :)

@laurentperez
Copy link

Hi

How would you pass the option in the command below ? Both do not produce builder code.

java -jar openapi-generator-cli.jar generate -g java -i xxxxx.json --additional-properties=generateBuilders=true -o .

java -jar openapi-generator-cli.jar generate -g java -i xxxx.json -p generateBuilders=true -o 

@jpfinne
Copy link
Contributor

jpfinne commented Sep 11, 2024

@laurentperez

You need to add a space after additional-properties:

java -jar openapi-generator-cli.jar generate -g java -i xxx.jason -o gen --additional-properties  generateBuilders=true --library resttemplate

Even if the code is in AbstractJavaCodeGen, not all libraries are tested with the bulder.
resttemplate, native, spring have a pojo.mustache containing these lines:

{{#generateBuilders}}
  {{>javaBuilder}}
{{/generateBuilders}}

See end of https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/native/pojo.mustache

If you want to use the builder in the default java library, you need to take a copy of https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/pojo.mustache
and to add the 3 lines with the builder partial.

This can be done with the template customization. See https://openapi-generator.tech/docs/templating/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[REQ] [java] create a builder pattern for the models
4 participants