Swagger Codegen (with maven plugin) for OpenAPI 3.0

Kuldeep Jain picture Kuldeep Jain · Apr 2, 2018 · Viewed 35.6k times · Source

I want to use Swagger Codegen for OpenAPI 3.0 YAML file. And I see Swagger Codegen 3.0.0-rc0 is available. But when I try to use that I run into issues. Following are the details:

My pom.xml file with swagger-codegen plugin:

<plugin>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>3.0.0-rc0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${basedir}/src/main/resources/mySpec.yaml</inputSpec>
        <output>target/generated-sources</output>
        <language>spring</language>
        <generateApis>false</generateApis>
        <modelPackage>com.kj.model</modelPackage>
        <apiPackage>com.kj</apiPackage>
        <configOptions>
          <sourceFolder>swagger</sourceFolder>
          <library>spring-mvc</library>
          <interfaceOnly>true</interfaceOnly>
          <useBeanValidation>true</useBeanValidation>
          <dateLibrary>java8</dateLibrary>
          <java8>true</java8>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>

With the above plugin when I run the maven build, I got this ServiceConfigurationError, here is the stack trace:

Exception in thread "main" java.util.ServiceConfigurationError: io.swagger.codegen.CodegenConfig: Provider io.swagger.codegen.languages.java.JavaClientCodegen not found
    at java.util.ServiceLoader.fail(ServiceLoader.java:239)
    at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:372)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at io.swagger.codegen.CodegenConfigLoader.forName(CodegenConfigLoader.java:19)
    at io.swagger.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:392)
    at io.swagger.codegen.plugin.CodeGenMojo.execute(CodeGenMojo.java:512)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)

In order to fix this I added swagger-codegen-generators dependency within the maven plugin section of pom file:

<dependencies>
  <dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-generators</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </dependency>
</dependencies>

So with this earlier mentioned issue got resolved but now I see this NPE

java.lang.NullPointerException
    at io.swagger.codegen.languages.SpringCodegen.preprocessOpenAPI(SpringCodegen.java:429)
    at io.swagger.codegen.DefaultGenerator.configureGeneratorProperties(DefaultGenerator.java:199)
    at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:716)
    at io.swagger.codegen.plugin.CodeGenMojo.execute(CodeGenMojo.java:534)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)

As you would have noted already that I am using <language>spring</language> and <library>spring-mvc</library>. Please let me know if codegen has worked for someone for 3.0.0-rc0 with these configurations.

Note: I looked at this old post which is similar but at that time 3.0.0-rc0 was not available.

Answer

William Cheng picture William Cheng · Jun 5, 2018

To use Swagger Codegen with Maven plug-in for OpenAPI 3.0.0 spec, you may consider using OpenAPI Generator instead (which is a community-driven version of Swagger Codegen).

<dependency>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>3.3.4</version>
</dependency>

Ref: https://github.com/OpenAPITools/openapi-generator#12---artifacts-on-maven-central

(please refer to the Q&A on why we forked Swagger Codegen)