Skip to content

Swagger Codegen Selective Generation

You may not want to generate all models in your project. Likewise you may want just one or two apis to be written. If that’s the case, you can use system properties to control the output:

The default is generate everything supported by the specific library. Once you enable a feature, it will restrict the contents generated:

Terminal window
1
# generate only models
2
java -Dmodels {opts}
3
4
# generate only apis
5
java -Dapis {opts}
6
7
# generate only supporting files
8
java -DsupportingFiles
9
10
# generate models and supporting files
11
java -Dmodels -DsupportingFiles

To control the specific files being generated, you can pass a CSV list of what you want:

Terminal window
1
# generate the User and Pet models only
2
-Dmodels=User,Pet
3
4
# generate the User model and the supportingFile `StringUtil.java`:
5
-Dmodels=User -DsupportingFiles=StringUtil.java

To control generation of docs and tests for api and models, pass false to the option. For api, these options are -DapiTests=false and -DapiDocs=false. For models, -DmodelTests=false and -DmodelDocs=false. These options default to true and don’t limit the generation of the feature options listed above (like -Dapi):

Terminal window
1
# generate only models (with tests and documentation)
2
java -Dmodels {opts}
3
4
# generate only models (with tests but no documentation)
5
java -Dmodels -DmodelDocs=false {opts}
6
7
# generate only User and Pet models (no tests and no documentation)
8
java -Dmodels=User,Pet -DmodelTests=false {opts}
9
10
# generate only apis (without tests)
11
java -Dapis -DapiTests=false {opts}
12
13
# generate only apis (modelTests option is ignored)
14
java -Dapis -DmodelTests=false {opts}

When using selective generation, only the templates needed for the specific generation will be used.

Ignore file format

Swagger Codegen supports a .swagger-codegen-ignore file, similar to .gitignore or .dockerignore you’re probably already familiar with.

The ignore file allows for better control over overwriting existing files than the --skip-overwrite flag. With the ignore file, you can specify individual files or directories can be ignored. This can be useful, for example if you only want a subset of the generated code.

Examples:

Terminal window
1
# Swagger Codegen Ignore
2
# Lines beginning with a # are comments
3
4
# This should match build.sh located anywhere.
5
build.sh
6
7
# Matches build.sh in the root
8
/build.sh
9
10
# Exclude all recursively
11
docs/**
12
13
# Explicitly allow files excluded by other rules
14
!docs/UserApi.md
15
16
# Recursively exclude directories named Api
17
# You can't negate files below this directory.
18
src/**/Api/
19
20
# When this file is nested under /Api (excluded above),
21
# this rule is ignored because parent directory is excluded by previous rule.
22
!src/**/PetApiTests.cs
23
24
# Exclude a single, nested file explicitly
25
src/IO.Swagger.Test/Model/AnimalFarmTests.cs

The .swagger-codegen-ignore file must exist in the root of the output directory.

Upon first code generation, you may also pass the CLI option --ignore-file-override=/path/to/ignore_file for greater control over generated outputs. Note that this is a complete override, and will override the .swagger-codegen-ignore file in an output directory when regenerating code.

Editor support for .swagger-codegen-ignore files is available in IntelliJ via the .ignore plugin.