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

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

Open
T-iny opened this issue Jun 15, 2018 · 16 comments

Comments

@T-iny
Copy link

T-iny commented Jun 15, 2018

Is there already have this function?

Thanks

@jmini
Copy link
Member

jmini commented Jun 15, 2018

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?

@T-iny
Copy link
Author

T-iny commented Jun 15, 2018

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

@jmini
Copy link
Member

jmini commented Jun 17, 2018

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.

@FearlessHyena
Copy link

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

@deviantlycan
Copy link
Contributor

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.

@LionH
Copy link
Contributor

LionH commented Mar 10, 2020

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

@qcastel
Copy link

qcastel commented Feb 24, 2021

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>

@eyalrin
Copy link

eyalrin commented Jul 27, 2021

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...

@melbeltagy
Copy link

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

@dineshgyl
Copy link

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?

@bharatnpti
Copy link

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

nilskuhn pushed a commit to nilskuhn/openapi-generator that referenced this issue Apr 6, 2023
…nrwl-monorepo

chore(deps): update nrwl monorepo to v12 (major)
@ericdriggs
Copy link

ericdriggs commented Nov 8, 2023

@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,

@Serob
Copy link

Serob commented Feb 20, 2024

@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?

@qcastel
Copy link

qcastel commented Feb 21, 2024

@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>

@melbeltagy
Copy link

@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.

@ericdriggs
Copy link

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

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

No branches or pull requests