20

I am using OpenAPI generator maven plugin like one below for generating Java client code for models .

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>4.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                <generatorName>java</generatorName>
                <configOptions>
                   <sourceFolder>src/gen/java/main</sourceFolder>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

When , I generate the model classes, they get generated with usual POJO field declarations and getters and setters. But what I want to do is, instead of generating getters and setters, I want my classes to get automatically generated with Lombok annotations for Java pojos like @Getter, @Setter, @Data, etc. Is there a way to customize model generator to fit above use case requirement?

I tried to find out if there is a way. I found this discussion, where the very last comment talks about a PR, where the issue of generating models using Lombok annotations has been addressed. But I do not see any clear indication of usage or any documentation of this feature in the OpenAPI generator open source project that it has been implemented yet. So, is there any way of generating models with Lombok annotations instead of regular getters and setters today?

DV82XL
  • 5,350
  • 5
  • 30
  • 59
Saurabh Chaturvedi
  • 2,028
  • 2
  • 18
  • 39
  • 1
    Perhaps it might be easier to code using the new [records](https://openjdek.java.net/jeps/395) feature arriving in Java 16, rather than Lombok. – Basil Bourque Jan 15 '21 at 23:55

3 Answers3

54

To complete this very old thread: Now it does support Lombok annotations.

Example taken from here

 <configOptions>
     <additionalModelTypeAnnotations>@lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor</additionalModelTypeAnnotations>
 </configOptions>
Laess3r
  • 944
  • 1
  • 9
  • 13
  • 1
    Great @Laess3r , thank you for commenting . It will help others too . – Saurabh Chaturvedi Apr 08 '21 at 11:08
  • 1
    Nice! In what version of openapi generator did they add Lombok support? – DV82XL May 25 '21 at 02:10
  • 1
    @DV82XL Seems like additional annotation support was introduced March 2020 in OpenAPI generator 4.2.3. – Laess3r May 27 '21 at 18:26
  • Tried 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... – Eyal Ringort Jul 27 '21 at 07:20
  • 3
    I either have no idea how or I am not seeing something, but this configOption does not work for me - the generated files still do not contain the annotations (nor the imports or anything). So is there really support for this in the `swagger-codegen-maven-plugin`? I tried the older v2 and the new v3 - without success. I even added `io.swagger.core.v3:swagger-annotations:2.1.9` to my `pom.xml´, but still without any success. Does someone have a complete working sample along with the version numbers, please? – Voronin Sep 20 '21 at 00:42
  • As work around regarding missing `lombok` dependencies in generated `build.gradle`, use Templates [https://openapi-generator.tech/docs/templating ] to generate your own version of `build.gradle` and add the required `lombok` dependencies – Mohamed El-Beltagy Nov 20 '21 at 00:25
  • on the CLI you can also add `--additional-properties=additionalModelTypeAnnotations="@lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor"` – rjdkolb Sep 14 '22 at 11:50
  • To avoid wanrings in the pom.xml, escape `@` with `@`: `@lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor` – andy Jan 30 '23 at 09:22
  • to address @Voronin question, this option is NOT available for swagger-codegen-maven-plugin , only for openapi-generator-maven-plugin . There doesn't seem to be support for Lombok in the swagger-codegen-maven-plugin - probably better to migrate to the openapi version. – Pierluigi Vernetto Jul 20 '23 at 12:44
3

EDIT: This answer is deprecated. See the post by @Laess3r. I'll leave this, since it is applicable for older versions of openapi generator.


openapi-generator does not yet support Lombok annotations. If you want to generate code with Lombok annotations, you need to create a custom template in mustache, as described in https://openapi-generator.tech/docs/templating/.

If you've never worked with mustache, be aware that it's somewhat hard to read, so try to keep the templates as simple as possible and make sure to add unit tests to validate the generated output. The template will look something like this:

/**
 * {{#description}}{{description}}{{/description}}
 */
@Data
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}} {
{{#vars}}
    /**
     * {{#description}}{{description}}{{/description}}
     */
    @JsonProperty("{{#lambda.lowercase}}{{nameInSnakeCase}}{{/lambda.lowercase}}")
    private {{{datatypeWithEnum}}} {{name}};
{{/vars}}
DV82XL
  • 5,350
  • 5
  • 30
  • 59
1

I've been able to get this working out-of-the-box using a space separated list of annotations on models:

@lombok.experimental.SuperBuilder @lombok.external.Jacksonized

If models have readOnly set to "true" the Builder becomes the only way to make the object and @Jacksonized allows it to be serialized/deserialized. There are some limitations with inheritance (turning off requiring all required parameters in the configOptions).