mustacheopenapi-generator

Mustache templates, list of options and formatting


I'm using mustache templates to generate Jetbrains HTTP Client files from OpenAPI specifications. (Though you don't need any of that context, my question is about mustache templates.)

In short, I can have zero, one or many security options and whatever the result I'd like each of the results to neatly be on a new line.

How to ensure that, if the options are false, the line breaks are ignored by the templating?

So far I haven't managed to format things as desired.

For example, for a query where isBasicBasic is true, isBasicBearer is false and isApiKey is true, using the following template:

{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}
{{#authMethods}}{{#isBasicBasic}}Authorization: Basic username password{{/isBasicBasic}}
{{#isBasicBearer}}Authorization: Bearer token{{/isBasicBearer}}
{{#isApiKey}}{{keyParamName}} apiKey{{/isApiKey}}{{/authMethods}}
{{#consumes}}Content-Type: {{{mediaType}}}
{{/consumes}}

The result I get at the moment is:

## BasicApi

### Get User
## Get User Info by User ID
# @name getUsersUserId
GET http://localhost:5001/v1/users/{{userId}}
Authorization: Basic username password



X-API-Key apiKey

while the desired result is:

## BasicApi

### Get User
## Get User Info by User ID
# @name getUsersUserId
GET http://localhost:5001/v1/users/{{userId}}
Authorization: Basic username password
X-API-Key apiKey

Putting everything on one line will remove the additional line breaks, but that is not helpful:

{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}
{{#authMethods}}{{#isBasicBasic}}Authorization: Basic username password{{/isBasicBasic}}{{#isBasicBearer}}Authorization: Bearer token{{/isBasicBearer}}{{#isApiKey}}{{keyParamName}} apiKey{{/isApiKey}}{{/authMethods}}
{{#consumes}}Content-Type: {{{mediaType}}}

returns:

### Get User
## Get User Info by User ID
# @name getUsersUserId
GET http://localhost:5001/v1/users/{{userId}}
Authorization: Basic username passwordX-API-Key apiKey

I am not generating HTML, meaning that I don't think I can insert manual line breaks like <br>. I tried manually inserting carriage returns, but they just render in text


Solution

  • Mustache has the concept of standalone tags. If a section or end section tag appears on a line by itself with nothing but whitespace, it is guaranteed not to leave an empty line in the output. Hence, you can write this instead:

    {{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}
    {{#authMethods}}
        {{#isBasicBasic}}
    Authorization: Basic username password
        {{/isBasicBasic}}
        {{#isBasicBearer}}
    Authorization: Bearer token
        {{/isBasicBearer}}
        {{#isApiKey}}
    {{keyParamName}} apiKey
        {{/isApiKey}}
    {{/authMethods}}
    {{#consumes}}
    Content-Type: {{{mediaType}}}
    {{/consumes}}
    

    You can try it out by copy-pasting the following savestate into the load/store box in the playground (see the second template):

    {"data":{"text":"{\n    httpMethod: 'GET',\n    basePath: 'http://localhost:5001/v1/',\n    path: 'users/{{userId}}',\n    lambda: {\n        doubleMustache(content) {\n            return true;\n        },\n    },\n    authMethods: [{\n        isBasicBasic: true,\n    }, {\n        isApiKey: true,\n        keyParamName: 'X-API-Key',\n    }],\n}"},"templates":[{"name":"original-solution","text":"{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}\n{{#authMethods}}{{#isBasicBasic}}Authorization: Basic username password\n{{/isBasicBasic}}{{#isBasicBearer}}Authorization: Bearer token\n{{/isBasicBearer}}{{#isApiKey}}{{keyParamName}} apiKey\n{{/isApiKey}}{{/authMethods}}\n{{#consumes}}Content-Type: {{{mediaType}}}{{/consumes}}"},{"text":"{{httpMethod}} {{basePath}}{{#lambda.doubleMustache}}{{path}}{{/lambda.doubleMustache}}\n{{#authMethods}}\n    {{#isBasicBasic}}\nAuthorization: Basic username password\n    {{/isBasicBasic}}\n    {{#isBasicBearer}}\nAuthorization: Bearer token\n    {{/isBasicBearer}}\n    {{#isApiKey}}\n{{keyParamName}} apiKey\n    {{/isApiKey}}\n{{/authMethods}}\n{{#consumes}}\nContent-Type: {{{mediaType}}}\n{{/consumes}}","name":"standalone-solution"}]}