Creating a new template

If none of the templates suit your needs, you can create a brand new template. OpenAPI Generator can help with this, using the meta command:

  1. java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar meta \
  2. -o out/generators/my-codegen -n my-codegen -p com.my.company.codegen

This will create a new directory out/generators/my-codegen, with all the files you need to get started - including a README.md. Once modified and compiled, you can use your new codegen just like any other, with your own custom-rolled logic.

These names can be anything you like. If you are building a client for the whitespace language, maybe you'd use the options -o out/generators/whitespace -n whitespace. They can be the same, or different, it doesn't matter. The -n value will be become the template name.

NOTE Convention is to use kebab casing for names passed to -n. Example, scala-finatra would become ScalaFinatraGenerator.

Use your new generator with the CLI

To compile your library, enter the out/generators/my-codegen directory, run mvn package and execute the generator:

  1. java -cp out/generators/my-codegen/target/my-codegen-openapi-generator-1.0.0.jar:modules/openapi-generator-cli/target/openapi-generator-cli.jar org.openapitools.codegen.OpenAPIGenerator

For Windows users, you will need to use ; instead of : in the classpath, e.g.

  1. java -cp out/generators/my-codegen/target/my-codegen-openapi-generator-1.0.0.jar;modules/openapi-generator-cli/target/openapi-generator-cli.jar org.openapitools.codegen.OpenAPIGenerator

Note the my-codegen is an option for -g now, and you can use the usual arguments for generating your code:

  1. java -cp out/codegens/customCodegen/target/my-codegen-openapi-generator-1.0.0.jar:modules/openapi-generator-cli/target/openapi-generator-cli.jar \
  2. org.openapitools.codegen.OpenAPIGenerator generate -g my-codegen \
  3. -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml \
  4. -o ./out/myClient

For Windows users:

  1. java -cp out/codegens/customCodegen/target/my-codegen-openapi-generator-1.0.0.jar;modules/openapi-generator-cli/target/openapi-generator-cli.jar \
  2. org.openapitools.codegen.OpenAPIGenerator generate -g my-codegen \
  3. -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml \
  4. -o ./out/myClient

Use your new generator with the maven plugin

Install your library to your local maven repository by running:

  1. mvn clean install -f out/generators/my-codegen

This will install org.openapitools:my-codegen-openapi-generator:1.0.0 to your local maven repository.

You can use this as additional dependency of the openapi-generator-maven-plugin plugin and use my-codegen as generatorName value:

  1. <plugin>
  2. <groupId>org.openapitools</groupId>
  3. <artifactId>openapi-generator-maven-plugin</artifactId>
  4. <version>${openapi-generator-version}</version>
  5. <executions>
  6. <execution>
  7. <id>generate-client-code</id>
  8. <goals>
  9. <goal>generate</goal>
  10. </goals>
  11. <configuration>
  12. <generatorName>my-codegen</generatorName>
  13. <!-- other configuration ... -->
  14. </configuration>
  15. </execution>
  16. </executions>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.openapitools</groupId>
  20. <artifactId>my-codegen-openapi-generator</artifactId>
  21. <version>1.0.0</version>
  22. </dependency>
  23. </dependencies>
  24. </plugin>

If you publish your artifact to a distant maven repository, do not forget to add this repository as pluginRepository for your project.

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 models
  2. java -Dmodels {opts}
  3. # generate only apis
  4. java -Dapis {opts}
  5. # generate only supporting files
  6. java -DsupportingFiles
  7. # generate models and supporting files
  8. java -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 only
  2. -Dmodels=User,Pet
  3. # generate the User model and the supportingFile `StringUtil.java`:
  4. -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)
  2. java -Dmodels {opts}
  3. # generate only models (with tests but no documentation)
  4. java -Dmodels -DmodelDocs=false {opts}
  5. # generate only User and Pet models (no tests and no documentation)
  6. java -Dmodels=User,Pet -DmodelTests=false {opts}
  7. # generate only apis (without tests)
  8. java -Dapis -DapiTests=false {opts}
  9. # generate only apis (modelTests option is ignored)
  10. java -Dapis -DmodelTests=false {opts}

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

To skip models defined as the form parameters in "requestBody", please use skipFormModel (default to false) (this option is introduced at v3.2.2)

  1. java -DskipFormModel=true

This option will be helpful to skip model generation due to the form parameter, which is defined differently in OAS3 as there's no form parameter in OAS3

Ignore file format

OpenAPI Generator supports a .openapi-generator-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. # OpenAPI Generator Ignore
  2. # Lines beginning with a # are comments
  3. # This should match build.sh located anywhere.
  4. build.sh
  5. # Matches build.sh in the root
  6. /build.sh
  7. # Exclude all recursively
  8. docs/**
  9. # Explicitly allow files excluded by other rules
  10. !docs/UserApi.md
  11. # Recursively exclude directories named Api
  12. # You can't negate files below this directory.
  13. src/**/Api/
  14. # When this file is nested under /Api (excluded above),
  15. # this rule is ignored because parent directory is excluded by previous rule.
  16. !src/**/PetApiTests.cs
  17. # Exclude a single, nested file explicitly
  18. src/Org.OpenAPITools.Test/Model/AnimalFarmTests.cs

The .openapi-generator-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 .openapi-generator-ignore file in an output directory when regenerating code.

Editor support for .openapi-generator-ignore files is available in IntelliJ via the .ignore plugin.

Customizing the generator

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:

  1. $ ls -1 modules/openapi-generator/src/main/java/org/openapitools/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.

  1. java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
  2. -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml \
  3. -g 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. }

You can use also config.yml with following equivalent example:

  1. apiPackage: "petstore"

Supported config options can be different per language. Running config-help -g {lang} will show available options.These options are applied via configuration file (e.g. config.json or config.yml) or by passing them with -D{optionName}={optionValue}. (If -D{optionName} does not work, please open a ticket and we'll look into it)

  1. java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar config-help -g java

Output

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

Or if you preffer yaml format it can look like

  1. groupId: "com.my.company"
  2. artifactId: "MyClient"
  3. artifactVersion: "1.2.0"
  4. library: "feign"

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.openapitools.codegen;
  2. import org.openapitools.codegen.languages.*;
  3. public class MyObjcCodegen extends ObjcClientCodegen {
  4. static {
  5. PREFIX = "HELO";
  6. }
  7. }

and specify the classname when running the generator:

  1. -g com.mycompany.openapitools.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 tellthe codegen what not to create. When doing this, every location that references a specific model willrefer 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:

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

Or for multiple mappings:

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

or

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