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:
1# generate only models2java -Dmodels {opts}3
4# generate only apis5java -Dapis {opts}6
7# generate only supporting files8java -DsupportingFiles9
10# generate models and supporting files11java -Dmodels -DsupportingFiles
To control the specific files being generated, you can pass a CSV list of what you want:
1# generate the User and Pet models only2-Dmodels=User,Pet3
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
):
1# generate only models (with tests and documentation)2java -Dmodels {opts}3
4# generate only models (with tests but no documentation)5java -Dmodels -DmodelDocs=false {opts}6
7# generate only User and Pet models (no tests and no documentation)8java -Dmodels=User,Pet -DmodelTests=false {opts}9
10# generate only apis (without tests)11java -Dapis -DapiTests=false {opts}12
13# generate only apis (modelTests option is ignored)14java -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:
1# Swagger Codegen Ignore2# Lines beginning with a # are comments3
4# This should match build.sh located anywhere.5build.sh6
7# Matches build.sh in the root8/build.sh9
10# Exclude all recursively11docs/**12
13# Explicitly allow files excluded by other rules14!docs/UserApi.md15
16# Recursively exclude directories named Api17# You can't negate files below this directory.18src/**/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.cs23
24# Exclude a single, nested file explicitly25src/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.