swagger-codegen contains a template-driven engine to generate documentation.
50K+

:star::star::star: If you would like to contribute, please refer to guidelines and a list of open tasks.:star::star::star:
:notebook_with_decorative_cover: For more information, please refer to the Wiki page and FAQ :notebook_with_decorative_cover:
:warning: If the OpenAPI/Swagger spec is obtained from an untrusted source, please make sure you've reviewed the spec before using Swagger Codegen to generate the API client, server stub or documentation as code injection may occur :warning:
:rocket: ProductHunt: https://producthunt.com/posts/swagger-codegen :rocket:
:notebook_with_decorative_cover: The eBook A Beginner's Guide to Code Generation for REST APIs is a good starting point for beginners.
NOTE: version 2.X (io.swagger) and 3.X (io.swagger.codegen.v3) have different group ids.
2.X and 3.X version lines of Swagger Codegen are available; 2.X (master branch) supports Swagger/OpenAPI version 2,
while 3.X (3.0.0 branch) 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.
NOTE: this document refers to version 2.X, check here for 3.X.
master branch)Swagger Codegen 2.X supports Swagger/OpenAPI version 2.
group id: io.swagger
maven central (maven plugin): https://mvnrepository.com/artifact/io.swagger/swagger-codegen-maven-plugin
dependency example:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.4.12</version>
</dependency>
3.0.0 branch)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.
group id: io.swagger.codegen.v3
maven central: https://mvnrepository.com/artifact/io.swagger.codegen.v3
dependency example:
<dependency>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.16</version>
</dependency>
This is the Swagger Codegen project, which allows generation of API client libraries (SDK generation), server stubs and documentation automatically given an OpenAPI Spec. Currently, the following languages/frameworks are supported:
Check out OpenAPI-Spec for additional information about the OpenAPI project.
The OpenAPI Specification has undergone 3 revisions since initial creation in 2010. The Swagger Codegen project has the following compatibilities with the OpenAPI Specification:
| Swagger Codegen Version | Release Date | OpenAPI Spec compatibility | Notes |
|---|---|---|---|
| 3.0.17-SNAPSHOT (current 3.0.0, upcoming minor release) SNAPSHOT | TBD | 1.0, 1.1, 1.2, 2.0, 3.0 | Minor release |
| 3.0.16 (current stable) | 2020-01-15 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.16 |
| 3.0.15 (current stable) | 2020-01-03 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.15 |
| 3.0.14 | 2019-11-16 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.14 |
| 3.0.13 | 2019-10-16 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.13 |
| 3.0.12 | 2019-10-14 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.12 |
| 3.0.11 | 2019-08-24 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.11 |
| 3.0.10 | 2019-07-11 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.10 |
| 3.0.9 | 2019-06-28 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.9 |
| 3.0.8 | 2019-04-25 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.8 |
| 3.0.7 | 2019-03-26 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.7 |
| 3.0.5 | 2019-02-18 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.5 |
| 3.0.4 | 2019-01-16 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.4 |
| 3.0.3 | 2018-11-30 | 1.0, 1.1, 1.2, 2.0, 3.0 | tag v3.0.3 |
| 3.0.2 | 2018-10-19 | 1.0, 1.1, 1.2, 2.0, 3.0 | Minor release |
| 3.0.1 | 2018-10-05 | 1.0, 1.1, 1.2, 2.0, 3.0 | Major release with breaking changes |
| 3.0.0 | 2018-09-06 | 1.0, 1.1, 1.2, 2.0, 3.0 | Major release with breaking changes |
| 2.4.13-SNAPSHOT (current master, upcoming minor release) SNAPSHOT | TBD | 1.0, 1.1, 1.2, 2.0 | Minor release |
| 2.4.12 (current stable) | 2020-01-15 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.12 |
| 2.4.11 (current stable) | 2020-01-03 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.11 |
| 2.4.10 | 2019-11-16 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.10 |
| 2.4.9 | 2019-10-14 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.9 |
| 2.4.8 | 2019-08-24 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.8 |
| 2.4.7 | 2019-07-11 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.7 |
| 2.4.6 | 2019-06-28 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.6 |
| 2.4.5 | 2019-04-25 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.5 |
| 2.4.4 | 2019-03-26 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.4 |
| 2.4.2 | 2019-02-18 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.2 |
| 2.4.1 | 2019-01-16 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.1 |
| 2.4.0 | 2018-11-30 | 1.0, 1.1, 1.2, 2.0 | tag v2.4.0 |
| 2.3.1 | 2018-01-17 | 1.0, 1.1, 1.2, 2.0 | tag v2.3.1 |
| 2.3.0 | 2017-12-21 | 1.0, 1.1, 1.2, 2.0 | tag v2.3.0 |
| 2.2.3 | 2017-07-15 | 1.0, 1.1, 1.2, 2.0 | tag v2.2.3 |
| 2.2.2 | 2017-03-01 | 1.0, 1.1, 1.2, 2.0 | tag v2.2.2 |
| 2.2.1 | 2016-08-07 | 1.0, 1.1, 1.2, 2.0 | tag v2.2.1 |
| 2.1.6 | 2016-04-06 | 1.0, 1.1, 1.2, 2.0 | tag v2.1.6 |
| 2.0.17 | 2014-08-22 | 1.1, 1.2 | tag v2.0.17 |
| 1.0.4 | 2012-04-12 | 1.0, 1.1 | tag v1.0.4 |
If you're looking for the latest stable version, you can grab it directly from Maven.org (Java 8 runtime at a minimum):
wget http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.12/swagger-codegen-cli-2.4.12.jar -O swagger-codegen-cli.jar
java -jar swagger-codegen-cli.jar help
For Windows users, you will need to install wget or you can use Invoke-WebRequest in PowerShell (3.0+), e.g. Invoke-WebRequest -OutFile swagger-codegen-cli.jar http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.12/swagger-codegen-cli-2.4.12.jar
On a mac, it's even easier with brew:
brew install swagger-codegen
To build from source, you need the following installed and available in your $PATH:
Don't forget to install Java 8+.
Export JAVA_HOME in order to use the supported Java version:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
export PATH=${JAVA_HOME}/bin:$PATH
After cloning the project, you can build it from source with this command:
mvn clean package
If you don't have maven installed, you may directly use the included maven wrapper, and build with the command:
./mvnw clean package
To install, run brew install swagger-codegen
Here is an example usage:
swagger-codegen generate -i http://petstore.swagger.io/v2/swagger.json -l ruby -o /tmp/test/
You can use run-in-docker.sh to do all development. This script maps your local repository to /gen
in the docker container. It also maps ~/.m2/repository to the appropriate container location.
To execute mvn package:
git clone https://github.com/swagger-api/swagger-codegen
cd swagger-codegen
./run-in-docker.sh mvn package
Build artifacts are now accessible in your working directory.
Once built, run-in-docker.sh will act as an executable for swagger-codegen-cli. To generate code, you'll need to output to a directory under /gen (e.g. /gen/out). For example:
./run-in-docker.sh help # Executes 'help' command for swagger-codegen-cli
./run-in-docker.sh langs # Executes 'langs' command for swagger-codegen-cli
./run-in-docker.sh /gen/bin/go-petstore.sh # Builds the Go client
./run-in-docker.sh generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml \
-l go -o /gen/out/go-petstore -DpackageName=petstore # generates go client, outputs locally to ./out/go-petstore
Prerequisite: install Vagrant and VirtualBox.
git clone http://github.com/swagger-api/swagger-codegen.git
cd swagger-codegen
vagrant up
vagrant ssh
cd /vagrant
./run-in-docker.sh mvn package
The Swagger Generator image can act as a self-hosted web application and API for generating code. This container can be incorporated into a CI pipeline, and requires at least two HTTP requests and some docker orchestration to access generated code.
Example usage (note this assumes jq is installed for command line processing of JSON):
# Start container and save the container id
CID=$(docker run -d swaggerapi/swagger-generator)
# allow for startup
sleep 5
# Get the IP of the running container
GEN_IP=$(docker inspect --format '{{.NetworkSettings.IPAddress}}' $CID)
# Execute an HTTP request and store the download link
RESULT=$(curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
"swaggerUrl": "http://petstore.swagger.io/v2/swagger.json"
}' 'http://localhost:8188/api/gen/clients/javascript' | jq '.link' | tr -d '"')
# Download the generated zip and redirect to a file
curl $RESULT > result.zip
# Shutdown the swagger generator image
docker stop $CID && docker rm $CID
In the example above, result.zip will contain the generated client.
The Swagger Codegen image acts as a standalone executable. It can be used as an alternative to installing via homebrew, or for developers who are unable to install Java or upgrade the installed version.
To generate code with this image, you'll need to mount a local location as a volume.
Example:
docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate \
-i http://petstore.swagger.io/v2/swagger.json \
-l go \
-o /local/out/go
(On Windows replace ${PWD} with %CD%)
The generated code will be located under ./out/go in the current directory.
To generate a PHP client for http://petstore.swagger.io/v2/swagger.json, please run the following
git clone https://github.com/swagger-api/swagger-codegen
cd swagger-codegen
mvn clean package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
-i http://petstore.swagger.io/v2/swagger.json \
-l php \
-o /var/tmp/php_api_client
(if you're on Windows, replace the last command with java -jar modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i http://petstore.swagger.io/v2/swagger.json -l php -o c:\temp\php_api_client)
You can also download the JAR (latest release) directly from maven.org
To get a list of general options available, please run java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar help generate
To get a list of PHP specified options (which can be passed to the generator with a config file via the -c option), please run java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l php
You can build a client against the swagger sample petstore API as follows:
./bin/java-petstore.sh
(On Windows, run .\bin\windows\java-petstore.bat instead)
This will run the generator with this command:
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
-i http://petstore.swagger.io/v2/swagger.json \
-l java \
-o samples/client/petstore/java
with a number of options. You can get the options with the help generate command (below only shows partial results):
NAME
swagger-codegen-cli generate - Generate code with chosen lang
SYNOPSIS
swagger-codegen-cli generate
[(-a <authorization> | --auth <authorization>)]
[--additional-properties <additional properties>...]
[--api-package <api package>] [--artifact-id <artifact id>]
[--artifact-version <artifact version>]
[(-c <configuration file> | --config <configuration file>)]
[-D <system properties>...] [--git-repo-id <git repo id>]
[--git-user-id <git user id>] [--group-id <group id>]
[--http-user-agent <http user agent>]
(-i <spec file> | --input-spec <spec file>)
[--ignore-file-override <ignore file override location>]
[--import-mappings <import mappings>...]
[--instantiation-types <instantiation types>...]
[--invoker-package <invoker package>]
(-l <language> | --lang <language>)
[--language-specific-primitives <language specific primitives>...]
[--library <library>] [--model-name-prefix <model name prefix>]
[--model-name-suffi
Content type
Image
Digest
Size
196.5 MB
Last updated
over 6 years ago
docker pull apaleo/swagger-codegen