Skip to content

Latest commit

 

History

History
58 lines (40 loc) · 2.7 KB

standalone-generator-development.md

File metadata and controls

58 lines (40 loc) · 2.7 KB

Swagger Codegen 2.x Standalone generator development (separate project/repo)

As described in Readme, a new generator can be implemented by starting with a project stub generated by the meta command of swagger-codegen-cli.

This can be achieved without needing to clone the swagger-codegen repo, by downloading and running the jar, e.g.:

wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.19/swagger-codegen-cli-2.4.19.jar -O swagger-codegen-cli.jar
java -jar swagger-codegen-cli.jar meta -o output/myLibrary -n myClientCodegen -p com.my.company.codegen

Such generator can be then made available to the CLI by adding it to the classpath, allowing to run/test it via the command line CLI, add it to the build pipeline and so on, as mentioned in Readme.

Development in docker

Similar to what mentioned in Readme development in docker section, a standalone generator can be built and run in docker, without need of a java/maven environment on the local machine.

Generate the initial project:

# project dir
TARGET_DIR=/tmp/codegen/mygenerator
mkdir -p $TARGET_DIR
cd $TARGET_DIR
# generated code location
GENERATED_CODE_DIR=generated
mkdir -p $GENERATED_CODE_DIR
# download desired version
wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.19/swagger-codegen-cli-2.4.19.jar -O swagger-codegen-cli.jar
wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/master/standalone-gen-dev/docker-stub.sh -O docker-stub.sh
wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/master/standalone-gen-dev/generator-stub-docker.sh -O generator-stub-docker.sh
chmod +x *.sh
# generated initial stub: -p <root package> -n <generator name>
./generator-stub-docker.sh -p io.swagger.codegen.custom -n custom

A test definition if we don't have one:

wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -O petstore.yaml

Build the generator and run it against a definition (first time will be slower as it needs to download deps)

wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/master/standalone-gen-dev/run-in-docker.sh -O run-in-docker.sh
wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/master/standalone-gen-dev/docker-entrypoint.sh -O docker-entrypoint.sh
chmod +x *.sh
./run-in-docker.sh generate -i petstore.yaml -l custom -o /gen/$GENERATED_CODE_DIR