openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

How could I add @lombok.Builder or other annotation in the model object class in Java

Open T-iny opened this issue 7 years ago • 11 comments

Is there already have this function?

Thanks

T-iny avatar Jun 15 '18 03:06 T-iny

Well we do not rely on @lombok in our codebase (and personally I would not like to add it), but I guess you are generating some code (could you indicate which generator name you are using?) and you would like to use lombok in your generated project?

jmini avatar Jun 15 '18 05:06 jmini

Thanks @jmini, I am using command "spring" to generate model code. e.g openapi-generator generate -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g spring -o /tmp/test/

And I want the models for example “Pet” have default builders. Normally I will add the "@Builder" e.g now: image

expected: image

Thanks

T-iny avatar Jun 15 '18 06:06 T-iny

As I told, there is no lombok support right now...

You can still add the annotations to the generated project after code generation (see the guides project lombok website) and edit the generated files.

If you want to generate code with this annotation already in place, you will need to modify the templates. You can do this: just for you locally, or try to contribute it back to the project (then you will need to add an option, because this is not something everybody wants in its project).

Our customization doc is a good starting point. Do not hesitate to continue the discussion if you need more information.

jmini avatar Jun 17 '18 07:06 jmini

I think it would be good to have a way to add custom annotations while generating the models Other than Lombok other use-cases would adding JPA annotations like @Entity or @Table Being able to do this via the configuration file would really be ideal

FearlessHyena avatar Jun 10 '19 05:06 FearlessHyena

I did make a custom template set that allows you to optionally use Lombok for your models and to optionally include Actuator. You can find these here: https://github.com/deviantlycan/openapi-generator-templates/tree/master/generator-templates/JavaSpring/spring-boot-lombok-actuator

This may be a good inclusion to a new release, but I did not want to assume that something like this is on the roadmap.

deviantlycan avatar Nov 19 '19 23:11 deviantlycan

Thanks to this PR you can now use Additional annotations for model type like lombok in 4.2.3 release.

LionH avatar Mar 10 '20 18:03 LionH

Just to summarise this thread, here is an example of how to setup the maven plugin to generate models with lombok annotations:

 <configOptions>
                                <additionalModelTypeAnnotations>@lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor</additionalModelTypeAnnotations>
</configOptions>

qcastel avatar Feb 24 '21 22:02 qcastel

Hi, trying to use this additionalModelTypeAnnotations feature to add Lombok's annotations - When I use the Spring generator (Server) the generated classes indeed contain the annotations I specified. This is also the case with the Java generator for the client code, but the generated build.gradle of the client library doesn't include the needed Lombok dependencies so It cannot be built into a jar, thus making this feature unusable in client code generation...

eyalrin avatar Jul 27 '21 07:07 eyalrin

Hi, trying to use this additionalModelTypeAnnotations feature to add Lombok's annotations - When I use the Spring generator (Server) the generated classes indeed contain the annotations I specified. This is also the case with the Java generator for the client code, but the generated build.gradle of the client library doesn't include the needed Lombok dependencies so It cannot be built into a jar, thus making this feature unusable in client code generation...

As a work around, you can use Templates to generate your own version of build.gradle and add the required lombok dependencies

melbeltagy avatar Nov 20 '21 00:11 melbeltagy

Yes its working now but now I get this error

@data is only supported on a class

Is there a way to specify this annotation only for classes?

dineshgyl avatar Sep 01 '22 19:09 dineshgyl

how to use it with openapi-generator-cli-6.1.0.jar ?

bharatnpti avatar Sep 16 '22 13:09 bharatnpti

@bharatnpti use additionalModelTypeAnnotations. It's semicolon separated, surrounded by double quotes

java -jar openapi-generator-cli.jar generate \
   -i https://yourservice.com/v3/api-docs.yaml \
   -g java \
   --library native \
   -p your.package \
   --global-property apis,models,supportingFiles,apiTests=false,modelTests=false\
   --skip-validate-spec\
    -p additionalModelTypeAnnotations="@lombok.Data;@lombok.AllArgsConstructor;@lombok.Builder(toBuilder = true)"\
   --additional-properties apiPackage=your.package\
,hideGenerationTimestamp=true\
,invokerPackage=your.package.invoker\
,modelPackage=your.package.model\
,sourceFolder=src/gen/java,

ericdriggs avatar Nov 08 '23 16:11 ericdriggs

@qcastel @ericdriggs -p additionalModelTypeAnnotations="@lombok.Data;@lombok.AllArgsConstructor;@lombok.Builder(toBuilder = true)" actually does not remove getters, setters, hashCode, equals , constructors from generated model files. The purpose of provided annotations is to not have all that code written in the class. It adds the annotations but keeps the code as well. Why? Is that an expected behavior?

Serob avatar Feb 20 '24 23:02 Serob

@Serob I guess they just add those annotations without really parsing them.

I actually face the same issue than you when using it on a new project. I managed to get it working by adding the option generatedConstructorWithRequiredArgs to false. My maven plugin config looks like this:

 <configOptions>
      <generatedConstructorWithRequiredArgs>false</generatedConstructorWithRequiredArgs>
      <reactive>true</reactive>
      <delegatePattern>true</delegatePattern>
      <additionalModelTypeAnnotations>@lombok.Builder(toBuilder=true)
          @lombok.NoArgsConstructor @lombok.AllArgsConstructor
      </additionalModelTypeAnnotations>
  </configOptions>

qcastel avatar Feb 21 '24 20:02 qcastel

@Serob yes. This is the expected behavior.

In short, the openapi-generator does not support lombok or other annotations natively. So, as @qcastel mentioned, the generator is not aware of the meaning of those annotation and how they will affect the generated output. IMHO, it will be a lot of logic that is not related to the openapi-generator to start supporting different annotations natively. The generator just generates a POJO with the ability to apply additional annotations using additionalModelTypeAnnotations without having to modify the templates (if/when it makes sense).

So, in case of using lombok annotations, what I did, is that I customized the templates and removed the getXxx, setXxx, equals, and toString methods, as well as constructors.

It might look like a lot of work, but it's actually super easy to do (and maintain).
I'd suggest you give it a try.

melbeltagy avatar Feb 21 '24 22:02 melbeltagy

@Serob Personally, I don't worry about boilerplate on generated code, only functionality. ymmv.

ericdriggs avatar Feb 21 '24 23:02 ericdriggs