-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Expanding customization docs #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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`. | ||
|
|
||
| ### 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/ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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._ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be helpful to add something about running
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ``` | ||
|
|
||
|
|
||
|
|
@@ -95,7 +112,6 @@ The ignore file allows for better control over overwriting existing files than t | |
| Examples: | ||
|
|
||
| ```sh | ||
| # OpenAPI Generator Ignore | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
There was a problem hiding this comment.
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 withcsharp), 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
resourcedirectory" but we have many non-English speakers who will be confused by these three introductory sentences.