jknack / handlebars.java

Logic-less and semantic Mustache templates with Java
http://jknack.github.io/handlebars.java
Other
1.44k stars 384 forks source link

Type-safe templates are not type-safe #711

Open tobia opened 4 years ago

tobia commented 4 years ago

Currently type-safe templates still allows unknown variable names, with no error neither at compile time (compile) nor at runtime (apply) defeating the whole purpose of type safety.

It's not clear what this project's type-safe support even refers to.

Additionally, the following piece of documentation is wrong:

By default, Handlebars.java throws an java.lang.IllegalArgumentException() if a helper cannot be resolved.

Test:

public class Test {

    static class Data {
        Data(String name) { this.name = name; } 
        private String name; 
        public String getName() { return name; } 
        public void setName(String name) { this.name = name; }
    }

    interface DataTemplate extends TypeSafeTemplate<Data> { }

    public static void main(String[] args) throws IOException {
        Handlebars handlebars = new Handlebars();
        Template tpl = handlebars.compileInline("Hello, {{missingName}}!");
        DataTemplate dtpl = tpl.as(DataTemplate.class);
        Data data = new Data("World");
        System.out.println(dtpl.apply(data));
    }
}

Expected output: an exception, either at compile-time (preferred) or at runtime.
Actual output: Hello, !

GiganticRoss commented 4 years ago

aiya me too