samskivert / jmustache

A Java implementation of the Mustache templating language.
Other
834 stars 128 forks source link

linebreak is trimmed #126

Closed Zane-XY closed 9 months ago

Zane-XY commented 3 years ago

Trying to modify the template in the swagger code generator. The template is like

{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}}
  public {{{datatypeWithEnum}}} {{getter}}() {
    return {{name}};
  }

the expected result is

  @Min(1) @Max(99999)
  public Integer getZip() {
    return zip;
  }

however this gives

 @Min(1) @Max(99999)  public Integer getZip() {
    return zip;
  }

the line break after {{/useBeanValidation}} is not preserved.

agentgt commented 2 years ago

First see this: https://github.com/samskivert/jmustache#newline-trimming

Second I could not reproduce your problem.

     @Test
    public void testTrim()
            throws Exception {
        String template = //
                """
                        {{#useBeanValidation}}{{> beanValidation}}{{/useBeanValidation}}
                        public Integer getZip() {
                          return zip;
                        }""";

        var m = Map.of("useBeanValidation", true, "beanValidation", "@Validation");

        StringReader sr = new StringReader("@Min(1) @Max(99999)");
        Mustache.TemplateLoader loader = name -> sr;

        String actual = Mustache.compiler()
                .withLoader(loader)
                .compile(template).execute(m);

        String expected = "@Min(1) @Max(99999)\n" //  notice new line
                + "public Integer getZip() {\n"
                + "  return zip;\n"
                + "}";

        assertEquals(expected, actual);
    }
agentgt commented 9 months ago

@samskivert

I could not reproduce this. I think we can close this issue.