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.
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)
The v3 swagger codegen maven plugin released April 2019 generates working Java client libraries from an OpenAPI 3.0 spec, I'm using this Maven pom.xml plugin config:
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.8</version>
<executions>
..
All the rest of the configuration and configOptions entries are unchanged from version 2.4.5. I had to replace the old annotation dependency with the following so the client code would compile:
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.0.8</version>
</dependency>
HTH
来源:https://stackoverflow.com/questions/49616529/swagger-codegen-with-maven-plugin-for-openapi-3-0