OpenAPI CodeGen
Documentation
Config file DSL
Schema
Example
{
"type": "documentation",
"defaults": {
"exampleProperty": "exampleValue"
},
"directories": [
"docs"
],
"partials": {
"model": "model.mustache",
"operation": "operation.mustache"
},
"transformations": [
{ "template": "Hello from \{\{projectName\}\}", "output": "README.md" },
{ "input": "index.mustache", "output": "docs/index.html" }
]
}
Model properties
Predefined lambdas
Lambdas are special tags which invoke a predefined function named after the tag. The function receives the template fragment between the tags. More info about lambdas can be found here
We support the following pre-defined lambdas at the moment:
lambda | example | description |
---|---|---|
lowercase | {{#lowercase}}{{name}}{{/lowercase}} | Convert to lowercase |
uppercase | {{#uppercase}}{{name}}{{/uppercase}} | Convert to UPPERCASE |
snakecase | {{#snakecase}}{{name}}{{/snakecase}} | Convert to snake_case |
pascalcase | {{#pascalcase}}{{name}}{{/pascalcase}} | Convert to PascalCase |
camelcase | {{#camelcase}}{{name}}{{/camelcase}} | Convert to camelCase |
kebabcase | {{#kebabcase}}{{name}}{{/kebabcase}} | Convert to kebab-case |
Custom generators
Some languages have various reserved words or unusual way of formatting arguments. Sometimes custom lambdas are needed to solve the issues. A custom generator is simply a javascript module, for example:
const Hogan = require('hogan.js');
const RESERVED_WORDS = new Set([
'for'
]);
function sanitizeName(text) {
return "__"+text+"__"
}
function escapeReservedWord(text) {
return "'"+text+"'"
}
function hello_lambda() {
return function(text) {
return 'Hello ' + Hogan.compile(text).render(this);
}
}
function complex_lambda() {
return function(template) {
var path = Hogan.compile(template).render(this);
path = this.pathParams.reduce(function (acc, param) {
if (param.isPathParam) {
return acc.replace(
"{"+param.paramName+"}", param.paramNamePascalCase);
} else {
return acc.replace()
}
}, path);
return path.split("/").slice(1).join(", ")
}
}
module.exports = {
sanitizeName: sanitizeName,
reservedWords: RESERVED_WORDS,
escapeReservedWord: escapeReservedWord,
lambdas: {
hello: hello_lambda,
path_template: complex_lambda
}
};
The module has to be configured in your config under the generator
property. Here is an example:
{
"defaults": {
"modelNaming": "snake_case",
},
"generator": "../mygenerator.js",
"partials": {
},
"directories": [ "src" ],
"transformations": [
],
"perApi": [
],
"perModel": [
]
}