3

I try to use openapi-generator-maven-plugin together with Spring Boot 2.4.3 to generate code by the specifications made in an openapi.yaml according to some examples on the internet whereby no example provided the complete dependencies necessary to include. The plugin generates a class named OpenAPIDocumentationConfig which internally uses the imported class springfox.documentation.spring.web.paths.RelativePathProvider.

AFAIK this class is deprecated in favour of class DefaultPathProvider but I can't find a repository with this new class.

Here's my POM:

<properties>
    <java.version>11</java.version>
    <springdoc.version>1.5.5</springdoc.version>
    <springfox.version>3.0.0</springfox.version>
</properties>

<dependencies>

    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <!-- OpenApi / Swagger -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-data-rest</artifactId>
        <version>${springdoc.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>${springdoc.version}</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-oas</artifactId>
        <version>3.0.0</version>
    </dependency>

    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>2.1.7</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox.version}</version>
    </dependency>

    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>jackson-databind-nullable</artifactId>
        <version>0.2.1</version>
    </dependency>

    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-core</artifactId>
        <version>3.0.0</version>
    </dependency>
</dependencies>

<build>
    <finalName>app</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>5.0.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatorName>spring</generatorName>
                        <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                        <!--<language>java</language>-->
                        <configOptions>
                            <sourceFolder>src/java/main</sourceFolder>
                            <output>${project.build.directory}/generated-sources</output>
                            <!--<output>${project.basedir}/generated-sources</output>-->
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

enter image description here

enter image description here


Using only the springdoc-openapi-ui dependency results in even more errors: enter image description here

With this updated POM:

<properties>
    <java.version>11</java.version>
    <springdoc.version>1.5.5</springdoc.version>
    <springfox.version>3.0.0</springfox.version>
    <openapi-generator.version>3.0.0</openapi-generator.version>
</properties>

<dependencies>

    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <!-- OpenApi / Swagger -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-data-rest</artifactId>
        <version>${springdoc.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>${springdoc.version}</version>
    </dependency>

<!--        <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>2.1.7</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-core</artifactId>
        <version>3.0.0</version>
    </dependency>
-->
    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>jackson-databind-nullable</artifactId>
        <version>0.2.1</version>
    </dependency>

    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <finalName>app</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>4.3.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatorName>spring</generatorName>
                        <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                        <!--<language>java</language>-->
                        <configOptions>
                            <sourceFolder>src/java/main</sourceFolder>
                            <output>${project.build.directory}/generated-sources</output>
                            <!--<output>${project.basedir}/generated-sources</output>-->
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

The openapi.yaml specifying the code to be generated:

openapi: 3.0.3
info:
  title: Title
  description: "REST API Dokumentation xxx"
  version: ${artifactId}
  termsOfService: http://swagger.io/terms/
  contact:
    name: API Support
    email: xxx.yyy@zzz.de
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
#externalDocs:
servers:
  - url: http://{domain}:{port}
    description: The local server
    variables:
      domain:
        default: localhost
        description: api domain
      port:
        enum:
          - '8081'
        default: '8081'
paths:
  /api/hello:
    get:
      summary: Says 'hello' to the user.
      description: A test endpoint.
      parameters:
        - in: path
          name: name
          required: true
          schema:
            type: string
            description: The person's name to address to.
      responses:
        '200':
          description: Ok
        '500':
          description: Server error
        default:
          description: Unexpected error
          content:
            application/json:
              schema:
                type: string
du-it
  • 2,561
  • 8
  • 42
  • 80
  • No, unfortunately it's not as you can see from my edited question. It still doesn't build. – du-it Mar 10 '21 at 09:29
  • post your updated `pom.xml` here. – Sathiamoorthy Mar 10 '21 at 09:33
  • Updated POM posted. – du-it Mar 10 '21 at 10:11
  • `openapi-generator-maven-plugin` plugin not required. I have updated my answer with `pom.xml`, here I have added a plugin for `lombok`. please check – Sathiamoorthy Mar 10 '21 at 12:51
  • What is the advantage of the lombok plugin over the dependency? :-) With your changes I can conduct the build but I can't see the code generated that is specified in openapi.yaml (see updated question). – du-it Mar 10 '21 at 14:46
  • Lombok plugin is required on deployment. openapi.yaml is not all required when you use springdoc. Delete that openapi.yaml file – Sathiamoorthy Mar 10 '21 at 15:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229746/discussion-between-du-it-and-sathia). – du-it Mar 10 '21 at 18:41

2 Answers2

0

we can use useLombok and template with lombok

<useLombok>true</useLombok>
<templateDirectory>path to template folder</templateDirectory>

https://github.com/deviantlycan/openapi-generator-templates/tree/master/generator-templates/JavaSpring/spring-boot-lombok-actuator

-1

In your pom file, you are included all different openapis like springfox, springdoc, swaggerv3 but it shouldn't be. Use any one library.

My suggestion is to use springdoc which is required minimal configuration.

Remove all openapi dependencies in your pom.xml. Add the below dependencies alone in pom.xml.

<properties>
    <java.version>11</java.version>
    <springdoc.version>1.5.5</springdoc.version>
    <springfox.version>3.0.0</springfox.version>
    <openapi-generator.version>3.0.0</openapi-generator.version>
</properties>

<dependencies>

    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <!-- OpenApi / Swagger -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-data-rest</artifactId>
        <version>${springdoc.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>${springdoc.version}</version>
    </dependency>

    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>jackson-databind-nullable</artifactId>
        <version>0.2.1</version>
    </dependency>

    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <finalName>app</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
            </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>
    </plugins>
</build>

application.yml

springdoc:
  swagger-ui.path: /app-api-docs.html

Access the spring doc here

http://localhost:8080/app-api-docs.html
Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77