samskivert / jmustache

A Java implementation of the Mustache templating language.
Other
827 stars 129 forks source link

Broken partial indentation with nested sections #181

Open jbortkiewicz opened 3 days ago

jbortkiewicz commented 3 days ago

Description:

When a partial contains multiple nested standalone sections,the indentation is rendered for each standalone section tag (opening and closing), causing an accumulated indentation preceding a next output.

Context:

I found this issue while trying to develop templates that produce "pretty" formatted code with openapi-generator. It is a very common use case (not only with openapi-generator), that partial contains several nested sections intended to implement complicated if-else conditions, thus this issue makes it impossible to generate a properly indented code.

To Reproduce

Affected versions: 1.16, master (00be427d531ef1529c176ec7e6eacd27824e0c30) Failing test cases:

  - name: Partial Nested Section Indentation
    desc: Multiple nested standalone sections should not change indentation
    data: { content: "value" }
    template: |
      \
       {{>partial}}
      /
    partials:
      partial: |
        |
        {{#content}}
        {{#content}}
        {{{.}}}
        {{/content}}
        {{/content}}
        |
    expected: |
      \
       |
       value
       |
      /

  - name: Partial Indentation With Empty Nested Sections
    desc: Empty nested standalone sections should not change indentation
    data: { content: "value" }
    template: |
      \
       {{>partial}}
      /
    partials:
      partial: |
        |
        {{#content}}
        {{#missing}}
        {{.}}
        {{/missing}}
        {{/content}}
        |
    expected: |
      \
       |
       |
      /
agentgt commented 3 days ago

I will probably rewrite the parsing of JMustache later this summer to match JStachio and will attempt to fix this bug next week.

In the meantime I recommend considering using JStachio zero dependency mode for OpenAPI. With the zero dep mode https://jstach.io/doc/jstachio/current/apidocs/#code_generation_mode which is designed for code generation in APT or OpenAPI projects. I tested and this bug does not happen with it.

@jskov I know was using it for that so they maybe able to help you.