Skip to content

Swagger Codegen Generators Configuration

There are different aspects of customizing the code generator beyond just creating or modifying templates. Each language has a supporting configuration file to handle different type mappings, etc:

Terminal window
1
$ ls -1 modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/
2
AbstractJavaJAXRSServerCodegen.java
3
AbstractTypeScriptClientCodegen.java
4
... (results omitted)
5
TypeScriptAngularClientCodegen.java
6
TypeScriptNodeClientCodegen.java

Each of these files creates reasonable defaults so you can get running quickly. But if you want to configure package names, prefixes, model folders, etc. you can use a json config file to pass the values.

Terminal window
1
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
2
-i https://petstore.swagger.io/v2/swagger.json \
3
-l java \
4
-o samples/client/petstore/java \
5
-c path/to/config.json

and config.json contains the following as an example:

1
{
2
"apiPackage" : "petstore"
3
}

Supported config options can be different per language. Running config-help -l {lang} will show available options. These options are applied via configuration file (e.g. config.json) or by passing them with java -jar swagger-codegen-cli.jar -D{optionName}={optionValue}.

If -D{optionName} does not work, please open a ticket and we’ll look into it.

Terminal window
1
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l java

Output

1
CONFIG OPTIONS
2
modelPackage
3
package for generated models
4
5
apiPackage
6
package for generated api classes
7
...... (results omitted)
8
library
9
library template (sub-template) to use:
10
jersey1 - HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2
11
jersey2 - HTTP client: Jersey client 2.6
12
feign - HTTP client: Netflix Feign 8.1.1. JSON processing: Jackson 2.6.3
13
okhttp-gson (default) - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1
14
retrofit - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1 (Retrofit 1.9.0)
15
retrofit2 - HTTP client: OkHttp 2.5.0. JSON processing: Gson 2.4 (Retrofit 2.0.0-beta2)
16
google-api-client - HTTP client: google-api-client 1.23.0. JSON processing: Jackson 2.8.9
17
rest-assured - HTTP client: rest-assured : 3.1.0. JSON processing: Gson 2.6.1. Only for Java8

Your config file for Java can look like

1
{
2
"groupId": "com.my.company",
3
"artifactId": "MyClient",
4
"artifactVersion": "1.2.0",
5
"library": "feign"
6
}

For all the unspecified options default values will be used.

Another way to override default options is to extend the config class for the specific language. To change, for example, the prefix for the Objective-C generated files, simply subclass the ObjcClientCodegen.java:

1
package com.mycompany.swagger.codegen;
2
3
import io.swagger.codegen.languages.*;
4
5
public class MyObjcCodegen extends ObjcClientCodegen {
6
static {
7
PREFIX = "HELO";
8
}
9
}

and specify the classname when running the generator:

Terminal window
1
-l com.mycompany.swagger.codegen.MyObjcCodegen

Your subclass will now be loaded and overrides the PREFIX value in the superclass.

Bringing your own models

Sometimes you don’t want a model generated. In this case, you can simply specify an import mapping to tell the codegen what not to create. When doing this, every location that references a specific model will refer back to your classes. Note, this may not apply to all languages…

To specify an import mapping, use the --import-mappings argument and specify the model-to-import logic as such:

Terminal window
1
--import-mappings Pet=my.models.MyPet

Or for multiple mappings:

Terminal window
1
--import-mappings Pet=my.models.MyPet,Order=my.models.MyOrder

or

Terminal window
1
--import-mappings Pet=my.models.MyPet --import-mappings Order=my.models.MyOrder