handlebars.java
handlebars.java copied to clipboard
Different results from using Java 11 and 17
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)
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.