Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different results from using Java 11 and 17 #951

Open
tomasbjerre opened this issue Feb 27, 2022 · 1 comment
Open

Different results from using Java 11 and 17 #951

tomasbjerre opened this issue Feb 27, 2022 · 1 comment

Comments

@tomasbjerre
Copy link

tomasbjerre commented Feb 27, 2022

I am getting different results when using Java 11 and 17. I am not sure if it is an issue with this library, or a "feature" in Java 17...

Here is the code, also available here:
https://github.com/tomasbjerre/handlebars-issue

dependencies {
 api 'com.github.jknack:handlebars:4.3.0'

 testImplementation 'junit:junit:4.12'
 testImplementation 'org.slf4j:slf4j-simple:1.7.13'
 testImplementation 'org.assertj:assertj-core:2.3.0'
}

The assertions at the end of the test case shows how it differs.

import java.io.StringWriter;
import java.io.Writer;

import org.junit.Test;

import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;

public class TemplatesTest {

  public class Issue {
    private final boolean theVal;

    public Issue() {
      this.theVal = true;
    }

    public boolean isTheVal() {
      return this.theVal;
    }
  }

  @Test
  public void test() throws Exception {
    final Handlebars handlebars = new Handlebars();
    final Template template =
        handlebars.compileInline("{{isTheVal}}\n" + "isTheVal: {{#isTheVal}}yes{{/isTheVal}}\n");
    final Object context = new Issue();

    final Context changelogContext = Context.newContext(context);
    final Writer writer = new StringWriter();
    template.apply(changelogContext, writer);
    final String rendered = writer.toString();

    // Java 17
    // assertThat(rendered).isEqualTo("true\n" + "isTheVal: yes\n");
    // Java 11
    assertThat(rendered).isEqualTo("\n" + "isTheVal: \n");
  }
}

Java versions tested

$ javac -version
javac 17.0.1
$ java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 17.0.1+12-Ubuntu-120.04, mixed mode, sharing)
$ javac -version
javac 11.0.13
$ java -version
openjdk version "11.0.13" 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
@agentgt
Copy link
Contributor

agentgt commented Mar 30, 2022

The default value resolvers are different when using different JDKs. See https://github.com/jknack/handlebars.java/blob/master/handlebars/src/main/java/com/github/jknack/handlebars/ValueResolver.java#L87

@jknack This changed seemed to cause lots of confusion. Maybe some doc needs to be added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants