Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions docs/customization.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,55 @@
## Customization
### Modifying the client library format

Don't like the default client syntax? Want a different language supported? No problem! OpenAPI Generator processes mustache templates with the [jmustache](https://github.com/samskivert/jmustache) engine. You can modify our templates or make your own.
### Modifying a template

You can look at `modules/openapi-generator/src/main/resources/${your-language}` for examples. To make your own templates, create your own files and use the `-t` flag to specify your template folder. It actually is that easy.
Clone OpenAPI Generator and take a look at the following directory: `modules/openapi-generator/src/main/resources/${template}`. In here you'll see all of the generators available, for most programming languages, web application frameworks and web servers. For example, if you are looking for the C# template, it's named `csharp`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be updated. You're pointing the user to:

modules/openapi-generator/src/main/resources/csharp (where the reader will intuit ${template} is replaced with csharp), and saying that this includes "all of the generators available".

The "all of the generators available" is up a directory, at modules/openapi-generator/src/main/resources.

So, either ${template} would need to be removed, or the description about the directory needs to be modified to accommodate (That is, something like "in here, you'll find all of the templates specific to the generator you plan to modify").

English speakers will read this and go "Oh, that refers to the resource directory" but we have many non-English speakers who will be confused by these three introductory sentences.


### Making your own codegen modules
Templates consist of multiple mustache files. [Mustache](https://mustache.github.io/) is used as the templating language for these templates, and the specific engine used is [jmustache](https://github.com/samskivert/jmustache).

If you're starting a project with a new language and don't see what you need, openapi-generator can help you create a project to generate your own libraries:
If you wish to modify one of these templates, copy and paste the template you're interested in to a templates directory you control. To let OpenAPI Generator know where this templates directory is, use the `-t` option (e.g: `-t ./templates/`).

To tie that all together:

```sh
mkdir templates
cp -r modules/openapi-generator/src/main/resources/${template} templates/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's helpful to add to this doc or what… but if you install refined github in Google Chrome, you get a nifty Download button under every folder as you browse GitHub. I use this often, as you can easily browse to your desired generator that you'd like to extend under modules/openapi-generator/src/main/resources/ and download the folder directly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't wanna push people towards a browser extension when a single command would also explain things just fine.

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
-t ./templates/ -g ruby -i ./foo.yml -o ./out/ruby
```

_**Note:** You cannot use this approach to create new templates, only override existing ones._

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be helpful to add something about running new.sh in the project root for contributing new templates.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add that note after this PR? Not sure what you'd like to say.


### Creating a new template

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

```sh
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar meta \
-o output/myLibrary -n myClientCodegen -p com.my.company.codegen
-o out/codegens/customCodegen -n myCodegen -p com.my.company.codegen
```

This will write, in the folder `output/myLibrary`, all the files you need to get started, including a `README.md. Once modified and compiled, you can load your library with the codegen and generate clients with your own, custom-rolled logic.
This will create a new directory `out/codegens/customCodegen`, 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.

You would then compile your library in the `output/myLibrary` folder with `mvn package` and execute the codegen like such:
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/codegens/whitespace -n whitespace`. They can be the same, or different, it doesn't matter. The `-n` value will be become the template name.

To compile your library, enter the `out/codegens/customCodegen` directory, run `mvn package` and execute the generator:

```sh
java -cp output/myLibrary/target/myClientCodegen-openapi-generator-1.0.0.jar:modules/openapi-generator-cli/target/openapi-generator-cli.jar org.openapitools.codegen.OpenAPIGenerator
java -cp out/codegens/customCodegen/target/myCodegen-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.
```
java -cp output/myLibrary/target/myClientCodegen-openapi-generator-1.0.0.jar;modules/openapi-generator-cli/target/openapi-generator-cli.jar org.openapitools.codegen.OpenAPIGenerator
java -cp out/codegens/customCodegen/target/myCodegen-openapi-generator-1.0.0.jar;modules/openapi-generator-cli/target/openapi-generator-cli.jar org.openapitools.codegen.OpenAPIGenerator
```

Note the `myClientCodegen` is an option now, and you can use the usual arguments for generating your library:
Note the `myCodegen` is an option for `-g` now, and you can use the usual arguments for generating your code:

```sh
java -cp output/myLibrary/target/myClientCodegen-openapi-generator-1.0.0.jar:modules/openapi-generator-cli/target/openapi-generator-cli.jar \
io.openapitools.codegen.OpenAPIGenerator generate -g myClientCodegen\
java -cp out/codegens/customCodegen/target/myCodegen-openapi-generator-1.0.0.jar:modules/openapi-generator-cli/target/openapi-generator-cli.jar \
io.openapitools.codegen.OpenAPIGenerator generate -g myCodegen \
-i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml \
-o myClient
-o ./out/myClient
```


Expand Down Expand Up @@ -95,7 +112,6 @@ The ignore file allows for better control over overwriting existing files than t
Examples:

```sh
# OpenAPI Generator Ignore

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be removed, either. It's common for files to include a header explaining what the file is.

# Lines beginning with a # are comments

# This should match build.sh located anywhere.
Expand Down