Sign inSign up

apaleo/swagger-codegen

By apaleo

•Updated over 6 years ago

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

Image
1

50K+

apaleo/swagger-codegen repository overview

Build Status

Build Status

Build Status

  • Master (2.4.13-SNAPSHOT): Build Status Java Test Windows Test

  • 3.0.17-SNAPSHOT: Build Status Java Test Windows Test

Maven Central⁠

: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.

⁠Versioning

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.

⁠Swagger Codegen 2.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>
⁠Swagger Codegen 3.X (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>

⁠Overview

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:

  • API clients: ActionScript, Ada, Apex, Bash, C# (.net 2.0, 3.5 or later), C++ (cpprest, Qt5, Tizen), Clojure, Dart, Elixir, Elm, Eiffel, Erlang, Go, Groovy, Haskell (http-client, Servant), Java (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured), Kotlin, Lua, Node.js (ES5, ES6, AngularJS with Google Closure Compiler annotations) Objective-C, Perl, PHP, PowerShell, Python, R, Ruby, Rust (rust, rust-server), Scala (akka, http4s, swagger-async-httpclient), Swift (2.x, 3.x, 4.x, 5.x), Typescript (Angular1.x, Angular2.x, Fetch, jQuery, Node)
  • Server stubs: Ada, C# (ASP.NET Core, NancyFx), C++ (Pistache, Restbed), Erlang, Go, Haskell (Servant), Java (MSF4J, Spring, Undertow, JAX-RS: CDI, CXF, Inflector, RestEasy, Play Framework, PKMST⁠), Kotlin, PHP (Lumen, Slim, Silex, Symfony⁠, Zend Expressive⁠), Python (Flask), NodeJS, Ruby (Sinatra, Rails5), Rust (rust-server), Scala (Finch⁠, Lagom⁠, Scalatra)
  • API documentation generators: HTML, Confluence Wiki
  • Configuration files: Apache2⁠
  • Others: JMeter

Check out OpenAPI-Spec⁠ for additional information about the OpenAPI project.

⁠Table of contents

⁠Compatibility

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 VersionRelease DateOpenAPI Spec compatibilityNotes
3.0.17-SNAPSHOT (current 3.0.0, upcoming minor release) SNAPSHOT⁠TBD1.0, 1.1, 1.2, 2.0, 3.0Minor release
3.0.16⁠ (current stable)2020-01-151.0, 1.1, 1.2, 2.0, 3.0tag v3.0.16⁠
3.0.15⁠ (current stable)2020-01-031.0, 1.1, 1.2, 2.0, 3.0tag v3.0.15⁠
3.0.14⁠2019-11-161.0, 1.1, 1.2, 2.0, 3.0tag v3.0.14⁠
3.0.13⁠2019-10-161.0, 1.1, 1.2, 2.0, 3.0tag v3.0.13⁠
3.0.12⁠2019-10-141.0, 1.1, 1.2, 2.0, 3.0tag v3.0.12⁠
3.0.11⁠2019-08-241.0, 1.1, 1.2, 2.0, 3.0tag v3.0.11⁠
3.0.10⁠2019-07-111.0, 1.1, 1.2, 2.0, 3.0tag v3.0.10⁠
3.0.9⁠2019-06-281.0, 1.1, 1.2, 2.0, 3.0tag v3.0.9⁠
3.0.8⁠2019-04-251.0, 1.1, 1.2, 2.0, 3.0tag v3.0.8⁠
3.0.7⁠2019-03-261.0, 1.1, 1.2, 2.0, 3.0tag v3.0.7⁠
3.0.5⁠2019-02-181.0, 1.1, 1.2, 2.0, 3.0tag v3.0.5⁠
3.0.4⁠2019-01-161.0, 1.1, 1.2, 2.0, 3.0tag v3.0.4⁠
3.0.3⁠2018-11-301.0, 1.1, 1.2, 2.0, 3.0tag v3.0.3⁠
3.0.2⁠2018-10-191.0, 1.1, 1.2, 2.0, 3.0Minor release
3.0.1⁠2018-10-051.0, 1.1, 1.2, 2.0, 3.0Major release with breaking changes
3.0.0⁠2018-09-061.0, 1.1, 1.2, 2.0, 3.0Major release with breaking changes
2.4.13-SNAPSHOT (current master, upcoming minor release) SNAPSHOT⁠TBD1.0, 1.1, 1.2, 2.0Minor release
2.4.12⁠ (current stable)2020-01-151.0, 1.1, 1.2, 2.0tag v2.4.12⁠
2.4.11⁠ (current stable)2020-01-031.0, 1.1, 1.2, 2.0tag v2.4.11⁠
2.4.10⁠2019-11-161.0, 1.1, 1.2, 2.0tag v2.4.10⁠
2.4.9⁠2019-10-141.0, 1.1, 1.2, 2.0tag v2.4.9⁠
2.4.8⁠2019-08-241.0, 1.1, 1.2, 2.0tag v2.4.8⁠
2.4.7⁠2019-07-111.0, 1.1, 1.2, 2.0tag v2.4.7⁠
2.4.6⁠2019-06-281.0, 1.1, 1.2, 2.0tag v2.4.6⁠
2.4.5⁠2019-04-251.0, 1.1, 1.2, 2.0tag v2.4.5⁠
2.4.4⁠2019-03-261.0, 1.1, 1.2, 2.0tag v2.4.4⁠
2.4.2⁠2019-02-181.0, 1.1, 1.2, 2.0tag v2.4.2⁠
2.4.1⁠2019-01-161.0, 1.1, 1.2, 2.0tag v2.4.1⁠
2.4.0⁠2018-11-301.0, 1.1, 1.2, 2.0tag v2.4.0⁠
2.3.1⁠2018-01-171.0, 1.1, 1.2, 2.0tag v2.3.1⁠
2.3.0⁠2017-12-211.0, 1.1, 1.2, 2.0tag v2.3.0⁠
2.2.3⁠2017-07-151.0, 1.1, 1.2, 2.0tag v2.2.3⁠
2.2.2⁠2017-03-011.0, 1.1, 1.2, 2.0tag v2.2.2⁠
2.2.1⁠2016-08-071.0, 1.1, 1.2, 2.0tag v2.2.1⁠
2.1.6⁠2016-04-061.0, 1.1, 1.2, 2.0tag v2.1.6⁠
2.0.172014-08-221.1, 1.2tag v2.0.17⁠
1.0.42012-04-121.0, 1.1tag v1.0.4⁠
⁠Prerequisites

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:

⁠OS X Users

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
⁠Building

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
⁠Homebrew

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/
⁠Docker
⁠Development in docker

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
⁠Run Docker in Vagrant

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
⁠Public Pre-built Docker images
⁠Swagger Generator Docker Image

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.

⁠Swagger Codegen CLI Docker Image

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.

⁠Getting Started

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

⁠Generators

⁠To generate a sample client library

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

Tag summary

Content type

Image

Digest

Size

196.5 MB

Last updated

over 6 years ago

docker pull apaleo/swagger-codegen