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.
Swagger Codegen is an open source project which allows generation of API client libraries (SDK generation), server stubs, and documentation automatically from an OpenAPI Specification.
The OpenAPI Generator can be used as command line tool or as plugin for build tools like Maven and Gradle. The pom. xml file below shows the integration of the OpenAPI Generator plugin in a Maven project. To generate the server code, you need to add a plugin definition similar to the one below.
This plugin enables your Swagger-annotated project to generate Swagger specs and customizable, templated static documents during the maven build phase.
OpenAPI and Swagger used to refer to the same thing. While there are differences today (OpenAPI refers to RESTful API design and Swagger refers to a set of SmartBear tools), this blog will use the terms interchangeably. If you develop software today, chances are you are developing web APIs as well.
Swagger Codegen 2.X supports OpenAPI version 3 (and version 2 via spec conversion to version 3) Online generator of version 3.X supports both generation from Swagger/OpenAPI version 2 (by using engine + generators of 2.X) and version 3 specifications.
I have an API that I'm trying to rewrite that uses the old swagger-maven-codegen 2.3.1 with a legacy version of Spring 1.5.9. I can see that when I run mvn package or mvn install , the plugin generates the interfaces that all the Controllers implement.
You can use the swagger-codegen-maven-plugin for integrating with your workflow, and generating any codegen target. Gradle Swagger Generator Plugin is available for generating source code and API document. To push the auto-generated SDK to GitHub, we provide git_push.sh to streamline the process. For example:
If you have a Mac or a Linux environment, then you could use Homebrew to install the Swagger Codegen. All versions of the Swagger Codegen project can be found on Maven Central . Visit this folder on Maven, and choose the appropriate version (we recommend the latest version).
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>
Per request from @kozla13 below I added a complete POM example.
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- This groupId will NOT allow deployment in LF -->
<groupId>org.example.swaggerapi.client</groupId>
<artifactId>swagger-client</artifactId>
<name>Example</name>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<client.base.package.name>org.example.so</client.base.package.name>
</properties>
<dependencies>
<!-- Required for Java 9 and later -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!-- HTTP client: Spring RestTemplate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!-- JSON processing: jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.9</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- This 2019 version is required for OpenAPI 3 -->
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.8</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/ric-plt-a1/a1/openapi.yaml</inputSpec>
<language>java</language>
<packageName>${client.base.package.name}</packageName>
<modelPackage>${client.base.package.name}.model</modelPackage>
<apiPackage>${client.base.package.name}.api</apiPackage>
<invokerPackage>${client.base.package.name}.invoker</invokerPackage>
<configOptions>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<artifactVersion>${project.version}</artifactVersion>
<library>resttemplate</library>
<java8>true</java8>
<dateLibrary>java8</dateLibrary>
<licenseName>Apache 2.0</licenseName>
<licenseUrl>https://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With