Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openapi codegen without access methods

I am using the openapi-generator-maven-plugin to generate model sources.

Is there a way to generate them only with fields and without any access methods? I want the access methods to be generated via lombok with the additionalModelTypeAnnotations configOption in the maven configuration of the openapi-generator-maven-plugin

like image 494
nixfuerdiecharts Avatar asked Sep 08 '26 21:09

nixfuerdiecharts


2 Answers

As @beppe suggested in their answer, this can easily be done by modifying the mustache template. However, I highly advice against it. In doing so, you discard many of the the features included with the model objects created by the generator. Some of the features you lose are:

  • Bean Validation
  • Jackson Serialization Library Support
  • Support for vendor extensions

By adding the Lombok annotation to the generated files, you are basically ignoring the main point of the generator, which is to generate files that support each other. You are instead using a generator to call a generator.

Finally, the amount of customization necessary to make the lombok library work with the mustache files is much more than is really worth it. Sure, you can add @Data or @Jacksonized to your model, but what about @JsonIgnore? Do you want to use parameters in your Lombok calls? Because you can't set the builder classname via @Builder(builderClassName = "EmployeeBuilder"). That would be hardcoded. Instead, you'd have to use mustache parameters, such as @Builder(builderClassName = {{$builderClassName}}), which you'd then have to define elsewhere. What about @Builder vs @SuperBuilder? Which one do you use in which situation? How do you define and template it?

At this point, you might as well rewrite the entire pojo.mustache to be able to use the lombok annotations. But, what if you need the old pojo.mustache functionality elsewhere? Now you need to write a custom generator to determine which mustache to use in which situations.

It is best to just build the domain models as the openapi-generator intends and use them as they are. You can add annotations if you need to via vendor extensions such as x-field-extra-annotation and x-class-extra-annotation if you really feel the need to.

like image 163
tbatch Avatar answered Sep 10 '26 11:09

tbatch


You can implement those customizations by changing the Mustache templates

Fetch the templates of the Java framework (ie spring-boot) you want to use (ie openapi-generator-cli author template -g spring --additional-properties=library=spring-boot -o tmp/mytemplates

Modify the local model.mustache file to import the packages you want (lombok) and pojo.mustache to remove the getters/setters. There might other customisations necessary (each framework templates are different) but this the recommended approach.

like image 23
Beppe C Avatar answered Sep 10 '26 09:09

Beppe C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!