Saurabh Chaturvedi
Saurabh Chaturvedi

Reputation: 2166

Generating POJOs using OpenAPI generator with Lombok Annotations

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?

Upvotes: 26

Views: 44998

Answers (3)

user18020805
user18020805

Reputation: 11

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

Upvotes: 1

DV82XL
DV82XL

Reputation: 6639

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

Upvotes: 3

Laess3r
Laess3r

Reputation: 1153

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>

Upvotes: 69

Related Questions