Generating POJOs using OpenAPI generator with Lombok Annotations
Asked Answered
A

3

22

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?

Annabelannabela answered 15/1, 2021 at 9:55 Comment(1)
Perhaps it might be easier to code using the new records feature arriving in Java 16, rather than Lombok.Wurth
N
64

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>
Nahshon answered 8/4, 2021 at 5:53 Comment(8)
@DV82XL Seems like additional annotation support was introduced March 2020 in OpenAPI generator 4.2.3.Nahshon
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...Frumpish
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?Appoggiatura
As work around regarding missing lombok dependencies in generated build.gradle, use Templates [openapi-generator.tech/docs/templating ] to generate your own version of build.gradle and add the required lombok dependenciesSteppe
on the CLI you can also add --additional-properties=additionalModelTypeAnnotations="@lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor"Napoli
To avoid wanrings in the pom.xml, escape @ with &#64;: <additionalModelTypeAnnotations>&#64;lombok.Builder &#64;lombok.NoArgsConstructor &#64;lombok.AllArgsConstructor</additionalModelTypeAnnotations>Cimah
to address @Appoggiatura 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.Kally
But that's actually not removing getters, setters, hashCode, equals , constructors from generated model files. The purpose of provided annotations is to not ahve all that written in the class. It adds the annotations but keeps the code as well. Why?Thesis
K
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}}
Karsten answered 15/1, 2021 at 23:37 Comment(0)
M
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).

Milicent answered 22/3, 2022 at 23:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.