OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.52k stars 6.51k forks source link

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

Open T-iny opened 6 years ago

T-iny commented 6 years ago

Is there already have this function?

Thanks

jmini commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

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 commented 4 years ago

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 commented 4 years ago

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

qcastel commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

ericdriggs commented 11 months ago

@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 commented 7 months ago

@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 commented 7 months ago

@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 commented 7 months ago

@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 commented 7 months ago

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