Skip to content

Mustache Template Variables

William Cheng edited this page Oct 14, 2020 · 5 revisions

NOTE

This page is outdated, and superseded by the full doc site at https://openapi-generator.tech/


Mustache template variables in the model

To obtain a list of variables (tags) available in the mustache template. Please use debugOperations and debugModels. Heres is an example for variables in the model templates:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
  -i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml \
  -g csharp --global-property debugModels=true 

and here is part of the output:

############ Model info ############
[ {
  "model" : {
    "name" : "Order",
    "classname" : "Order",
    "classVarName" : "Order",
    "modelJson" : "{\n  \"type\" : \"object\",\n  \"properties\" : {\n    \"id\" : {\n      \"type\" : \"integer\",\n      \"format\" : \"int64\"\n    },\n    \"petId\" : {\n      \"type\" : \"integer\",\n      \"format\" : \"int64\"\n    },\n    \"quantity\" : {\n      \"type\" : \"integer\",\n      \"format\" : \"int32\"\n    },\n    \"shipDate\" : {\n      \"type\" : \"string\",\n      \"format\" : \"date-time\"\n    },\n    \"status\" : {\n      \"type\" : \"string\",\n      \"description\" : \"Order Status\",\n      \"enum\" : [ \"placed\", \"approved\", \"delivered\" ]\n    },\n    \"complete\" : {\n      \"type\" : \"boolean\",\n      \"default\" : false\n    }\n  },\n  \"xml\" : {\n    \"name\" : \"Order\"\n  }\n}",
    "vars" : [ {
      "baseName" : "id",
      "getter" : "getId",
      "setter" : "setId",
      "datatype" : "long?",
      "datatypeWithEnum" : "long?",
      "name" : "Id",
      "defaultValue" : "null",
      "baseType" : "long?",
      "jsonSchema" : "{\n  \"type\" : \"integer\",\n  \"format\" : \"int64\"\n}",
      "hasMore" : true,
      "isPrimitiveType" : true,
      "isNotContainer" : true,
      "isEnum" : false
    }, {

Mustache template variables in the operation

For operation (resource) templates, please use debugOperations instead:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
  -i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml \
  -g csharp --global-property debugOperations=true 

Here is part of the result

############ Operation info ############
[ {
  "packageName" : "Org.OpenAPITools",
  "hasImport" : true,
  "modelPackage" : "Org.OpenAPITools.Model",
  "package" : "Org.OpenAPITools.Api",
  "clientPackage" : "Org.OpenAPITools.Client",
  "version" : "1.0.0",
  "classVarName" : "user",
  "basePath" : "http://petstore.swagger.io/v2",
  "packageVersion" : "1.0.0",
  "infoEmail" : "[email protected]",
  "importPath" : "Org.OpenAPITools.Api.User",
  "licenseInfo" : "Apache 2.0",
  "hasMore" : "true",
  "generatedDate" : "2015-10-06T12:20:11.434+08:00",
  "classname" : "UserApi",
  "imports" : [ {
    "import" : "Org.OpenAPITools.Model.User"
  } ],
  "appName" : "Swagger Petstore",
  "appVersion" : "1.0.0",
  "generatorClass" : "class io.swagger.codegen.languages.CSharpClientCodegen",
  "baseName" : "User",
  "contextPath" : "/v2",
  "operations" : {
    "operation" : [ {
      "responseHeaders" : [ ],
      "hasProduces" : true,
      "hasParams" : true,
      "hasMore" : true,
      "isResponseBinary" : false,
      "path" : "/user",
      "operationId" : "CreateUser",
      "httpMethod" : "POST",
      "summary" : "Create user",
      "notes" : "This can only be done by the logged in user.",
      "baseName" : "User",
      "produces" : [ {
        "hasMore" : "true",
        "mediaType" : "application/xml"
      }, {
        "hasMore" : null,
        "mediaType" : "application/json"
      } ],
      "bodyParam" : {
        "isBodyParam" : true,
        "baseName" : "body",
        "paramName" : "body",
        "dataType" : "User",
        "description" : "Created user object",
        "jsonSchema" : "{\n  \"in\" : \"body\",\n  \"name\" : \"body\",\n  \"description\" : \"Created user object\",\n  \"required\" : true,\n  \"schema\" : {\n    \"$ref\" : \"#/definitions/User\"\n  }\n}",
        "isEnum" : false,
        "vendorExtensions" : { },
        "required" : true
      },
      "allParams" : [ {
        "isBodyParam" : true,
        "baseName" : "body",
        "paramName" : "body",
        "dataType" : "User",
        "description" : "Created user object",
        "jsonSchema" : "{\n  \"in\" : \"body\",\n  \"name\" : \"body\",\n  \"description\" : \"Created user object\",\n  \"required\" : true,\n  \"schema\" : {\n    \"$ref\" : \"#/definitions/User\"\n  }\n}",
        "isEnum" : false,
        "vendorExtensions" : { },
        "required" : true
      } ],
      "bodyParams" : [ {
        "isBodyParam" : true,
        "baseName" : "body",
        "paramName" : "body",
        "dataType" : "User",
        "description" : "Created user object",
        "jsonSchema" : "{\n  \"in\" : \"body\",\n  \"name\" : \"body\",\n  \"description\" : \"Created user object\",\n  \"required\" : true,\n  \"schema\" : {\n    \"$ref\" : \"#/definitions/User\"\n  }\n}",
        "isEnum" : false,
        "vendorExtensions" : { },
        "required" : true
      } ],
      "pathParams" : [ ],
      "queryParams" : [ ],
      "headerParams" : [ ],
      "formParams" : [ ],
      "tags" : [ "User" ],
      "responses" : [ {
        "headers" : [ ],
        "code" : "0",
        "message" : "successful operation",
        "hasMore" : false,
        "isDefault" : true,
        "simpleType" : true,
        "primitiveType" : true,
        "isMapContainer" : false,
        "isListContainer" : false,
        "isBinary" : false,
        "jsonSchema" : "{\n  \"description\" : \"successful operation\"\n}",
        "wildcard" : true
      } ],
      "imports" : [ "User" ],
      "vendorExtensions" : { },
      "nickname" : "CreateUser",
      "hasBodyParam" : true,
      "hasQueryParams" : false,
      "hasHeaderParams" : false,
      "hasPathParams" : false,
      "hasFormParams" : false
    }, {

Usage

For usage of the template variable, enter the variable name (e.g. basePath) in the search box and submit. Then filter by HTML & Django under Languages, e.g. https://github.com/openapitools/openapi-generator/search?l=html%2Bdjango&q=basepath&utf8=%E2%9C%93

Tips

To access the first or last element in a list

{{#vars}}{{#-first}} this is the first element {{.}} {{/-first}}{{/vars}}
{{#vars}}{{#-last}} this is the last element {{.}} {{/-last}}{{/vars}}

Explanations

Here are the explanations for some of the mustache tags:

  • complexType: stores the name of the model (e.g. Pet)
  • isContainer: true if the parameter or property is an array or a map.
  • isPrimitiveType: true if the parameter or property type is a primitive type (e.g. string, integer, etc) as defined in the spec.

Please feel free to update the list above with more tags. For any question, please open an issue.