Search code examples
spring-bootopenapi-generatorjava-17

Openapi generator maven plugin


I would like to try and use Openapi generator maven plugin with Spring Boot project.

Only example I can find by Googling is from Baeldung. But it simply is not possible to figure out the setup from that example. Example project is part of probably more than 1000 project chain dependencies ...

I know that this is not the forum for this kind of question, but this time only :) could someone point me to an example that demonstrates who to use the plugin with Spring Boot and java 17.

I get errorrs like

package javax.servlet.http does not exist

My latest current POM trial :

<?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.0.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <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.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>0.2.1</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>



        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>

        <dependency>
            <groupId>jakarta.annotation</groupId>
            <artifactId>jakarta.annotation-api</artifactId>
            <version>1.3.5</version>
        </dependency>

        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>5.1.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                /Users/ok/dev/openapi/demo/src/main/resources/petstore.yaml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.example.demo.api</apiPackage>
                            <modelPackage>com.example.demo.model</modelPackage>
                            <supportingFilesToGenerate>
                                ApiUtil.java
                            </supportingFilesToGenerate>
<!--
                            <configOptions>
                                <delegatePattern>true</delegatePattern>
                            </configOptions>
-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

Solution

  • Please, Try mine

    • recent
    • pom only (swagger plugin, not open-api's ... it was no "taste decision", but just what i had recently;)
    • openapi-v3
    • sample yaml/json is from petstore.swagger (links below)
    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.example</groupId>
      <artifactId>swagger-codegen-spring</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
      <name>swagger-codegen-spring</name>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
      </properties>
    
      <build>
        <plugins>
          <plugin>
            <groupId>io.swagger.codegen.v3</groupId> <--! v3 -->
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>3.0.42</version>
            <executions>
              <execution>
                <!-- prints (only, additionally) language specific(!) "config options help" -->
                <id>api-generate-help</id> 
                <goals>
                  <goal>generate</goal>
                </goals>
                <phase>generate-resources</phase>
                <configuration>
                  <inputSpec>src/main/resources/openapi.yaml</inputSpec>
                  <!-- language/generator!! -->
                  <language>spring</language>
                  <configHelp>true</configHelp>
                </configuration>
              </execution>
              <!-- this is the generation: -->
              <execution>
                <id>api-generate</id>
                <goals>
                  <goal>generate</goal>
                </goals>
                <phase>generate-sources</phase>
                <configuration>
                  <!-- input api definition -->
                  <inputSpec>src/main/resources/openapi.yaml</inputSpec>
                  <!-- language/generator!! -->
                  <language>spring</language>
                  <!-- output folder -->
                  <output>${project.build.directory}/gen</output>
                  <!-- ... -->
                  <configOptions> <!-- these are language specific: -->
                    <interfaceOnly>false</interfaceOnly>
                    <java8>false</java8>
                    <dateLibrary>java8</dateLibrary>
                    <!--sourceFolder>.</sourceFolder-->
                    <throwsException>true</throwsException>
                    <useTags>true</useTags>
                    <useBeanValidation>true</useBeanValidation>
                  </configOptions>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    After:

    mvn generate-sources 
    # or idempotent e.g. package, install, ...
    

    We get a "generated project" in (the configured) target/gen folder.

    This project is:

    • very language/generator specific
    • independent from the "parent" and has it's own pom (usually)
    • i would not mix it into (root) directory of an existing pom

    API definition links:

    Stay up-to-date

    And to be fair, also the "concurrence":

    Also "works":

    <plugin>
      <groupId>org.openapitools</groupId>
      <artifactId>openapi-generator-maven-plugin</artifactId>
      <version>6.5.0</version> <!-- latest -->
      <executions>
        <execution>
          <goals>
            <goal>generate</goal>
          </goals>
          <configuration>
            <inputSpec>
              https://petstore3.swagger.io/api/v3/openapi.yaml
            </inputSpec>
            <generatorName>spring</generatorName>
            <output>${project.build.directory}/gen-openapi</output>
          </configuration>
        </execution>
      </executions>
    </plugin>