UNPKG

3.38 kBMarkdownView Raw
1# OpenAPI CodeGen
2
3## Documentation
4
5### Config file DSL
6
7#### Schema
8
9[JSON Schema Draft 4](https://raw.githubusercontent.com/Mermade/openapi-codegen/master/schemas/config.json)
10
11#### Example
12
13```json
14{
15 "type": "documentation",
16 "defaults": {
17 "exampleProperty": "exampleValue"
18 },
19 "directories": [
20 "docs"
21 ],
22 "partials": {
23 "model": "model.mustache",
24 "operation": "operation.mustache"
25 },
26 "transformations": [
27 { "template": "Hello from \{\{projectName\}\}", "output": "README.md" },
28 { "input": "index.mustache", "output": "docs/index.html" }
29 ]
30}
31```
32
33### Model properties
34
35* [Defaults](modelProperties.md)
36
37### Predefined lambdas
38
39Lambdas are special tags which invoke a predefined function named after the tag.
40The function receives the template fragment between the tags. More info
41about lambdas can be found [here](https://github.com/OpenAPITools/openapi-generator/blob/master/docs/templating.md#mustache-lambdas)
42
43We support the following pre-defined lambdas at the moment:
44
45| lambda | example | description |
46|------------|----------------------------------------|-----------------------|
47| lowercase | \{\{#lowercase\}\}\{\{name\}\}\{\{/lowercase\}\} | Convert to lowercase |
48| uppercase | \{\{#uppercase\}\}\{\{name\}\}\{\{/uppercase\}\} | Convert to UPPERCASE |
49| snakecase | \{\{#snakecase\}\}\{\{name\}\}\{\{/snakecase\}\} | Convert to snake_case |
50| pascalcase | \{\{#pascalcase\}\}\{\{name\}\}\{\{/pascalcase\}\} | Convert to PascalCase |
51| camelcase | \{\{#camelcase\}\}\{\{name\}\}\{\{/camelcase\}\} | Convert to camelCase |
52| kebabcase | \{\{#kebabcase\}\}\{\{name\}\}\{\{/kebabcase\}\} | Convert to kebab-case |
53
54### Custom generators
55
56Some languages have various reserved words or unusual way of formatting arguments.
57Sometimes custom lambdas are needed to solve the issues.
58A custom generator is simply a javascript module, for example:
59
60```js
61const Hogan = require('hogan.js');
62
63const RESERVED_WORDS = new Set([
64 'for'
65]);
66
67function sanitizeName(text) {
68 return "__"+text+"__"
69}
70
71function escapeReservedWord(text) {
72 return "'"+text+"'"
73}
74
75function hello_lambda() {
76 return function(text) {
77 return 'Hello ' + Hogan.compile(text).render(this);
78 }
79}
80
81function complex_lambda() {
82 return function(template) {
83 var path = Hogan.compile(template).render(this);
84 path = this.pathParams.reduce(function (acc, param) {
85 if (param.isPathParam) {
86 return acc.replace(
87 "{"+param.paramName+"}", param.paramNamePascalCase);
88 } else {
89 return acc.replace()
90 }
91 }, path);
92 return path.split("/").slice(1).join(", ")
93 }
94}
95
96module.exports = {
97 sanitizeName: sanitizeName,
98 reservedWords: RESERVED_WORDS,
99 escapeReservedWord: escapeReservedWord,
100 lambdas: {
101 hello: hello_lambda,
102 path_template: complex_lambda
103 }
104};
105```
106
107The module has to be configured in your config under the `generator` property. Here is an example:
108```json
109{
110 "defaults": {
111 "modelNaming": "snake_case",
112 },
113 "generator": "../mygenerator.js",
114 "partials": {
115 },
116 "directories": [ "src" ],
117 "transformations": [
118 ],
119 "perApi": [
120 ],
121 "perModel": [
122 ]
123}
124```
125