Search code examples
javaspring-bootopenapiopenapi-generatoropenapi-generator-maven-plugin

Using Lombok in openapi generator


Description:

Unable to geenerate model classes with lombok extensions. The pom.xml configuration to support lombok annotation is not working.

openapi-generator-maven-plugin: 7.2.0
springdoc-openapi-starter-webmvc-ui: 2.1.0

OpenAPI declaration file content or url

openapi: 3.0.3
info:
  title: Example apis
  description: |-
    A simple collection of example APIs
  version: 1.0-SNAPSHOT
servers:
  - url: http://127.0.1:8091/api/v1
    description: Local server (uses test data)
  - url: https://example-dev.com/api/v1
    description: UAT server (uses test data)
  - url: https://example.com/api/v1
    description: Production server (uses live data)
tags:
  - name: example
    description: example apis
paths:
  /examples:
    get:
      summary: Get examples
      description: Get examples
      operationId: getExample
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Example'
        404:
          description: Leaderboard not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExampleNotFoundError'
  
components:
  schemas:
    Example:
      type: object
      properties:
        creatorId:
          type: string
        hipiCoins:
          type: number
    ExampleNotFoundError:
      type: object
      properties:
        creatorId:
          type: string
        hipiCoins:
          type: number

Generation Details

Language: Java
Libraries:
springdoc-openapi-starter-webmvc-ui: 2.1.0

I have pasted my pom.xml at the bottom which have all the details.

Steps to reproduce:

In the pom.xml @lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor not getting recognized, without this line everything is working fine.

Related issues: https://github.com/OpenAPITools/openapi-generator/issues/324#issuecomment-785438335

My pom.xml is

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.1</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.zee5.shorts</groupId>
  <artifactId>hipi-be-leaderboard</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>example</name>
  <description>Example Service</description>
  <properties>
    <java.version>21</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-jpa</artifactId>
      <version>3.2.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.openapitools</groupId>
      <artifactId>jackson-databind-nullable</artifactId>
      <version>0.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
      <version>2.1.0</version>
    </dependency>
    <dependency>
      <groupId>net.sourceforge.plantuml</groupId>
      <artifactId>plantuml-mit</artifactId>
      <version>1.2023.13</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>com.diffplug.spotless</groupId>
      <artifactId>spotless-maven-plugin</artifactId>
      <version>2.41.1</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.10.1</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>com.diffplug.spotless</groupId>
        <artifactId>spotless-maven-plugin</artifactId>
        <version>2.41.0</version>
        <configuration>
          <java>
            <includes>
              <include>src/main/java/**/*.java</include> <!-- Check application code -->
              <include>src/test/java/**/*.java</include> <!-- Check application tests code -->
            </includes>
            <googleJavaFormat>
              <version>1.17.0</version>
              <style>GOOGLE</style>
            </googleJavaFormat>
          </java>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
            </exclude>
          </excludes>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>7.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <skipValidateSpec>true</skipValidateSpec>
              <inputSpec>./src/main/resources/swagger-doc.yaml</inputSpec>
              <generatorName>spring</generatorName>
              <apiPackage>com.example.openapi.api</apiPackage>
              <modelPackage>com.example.openapi.model</modelPackage>
              <supportingFilesToGenerate>
                ApiUtil.java
              </supportingFilesToGenerate>
              <configOptions>
<additionalModelTypeAnnotations>@lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor</additionalModelTypeAnnotations>
                <delegatePattern>true</delegatePattern>
                <useSpringBoot3>true</useSpringBoot3>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

lombok.config

config.stopBubbling = true
lombok.experimental.flagUsage = warning

There are couple of blogs and stackoverflow posts similar to this, but none of the solution worked, might be some conflict with some other dependency, but not getting any error or exception.


Solution

  • @lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor these tags are not getting recognized by intellij but it is working.

    It is able to generate the classes with logbook annotations.