samskivert / jmustache

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

null value in nested context doesn't cover variable in outer context #40

Closed Tupteq closed 11 years ago

Tupteq commented 11 years ago

If variable is defined in inner context, but it's value is null, then outer context is searched.

Example: context (HashMap): {"a": "outer", "b": [{"a":null}]} Template: {{#b}}{{a}}{{/b}} Result: outer

Online JS tester returns no result here, so it probably should return defaultValue/nullValue (if set) or throw an exception. I also checked official Objective-C implementation and it worked as JS version.

If inner "a" is set to anything else, it behaves as expected.

samskivert commented 11 years ago

You must be using an earlier version of JMustache. This works as you specify in the latest code:

@Test public void testIssue40 () {
    String tmpl = "{{#b}}{{a}}{{/b}}";
    Object ctx = context("a", "outer", "b", context("a", null));
    // this one will use 'null' for the inner 'a' because nullValue() is supplied
    test(Mustache.compiler().nullValue("null"), "null", tmpl, ctx);
    // this one will throw a MustacheException because no nullValue() is supplied
    try {
        test(Mustache.compiler(), "", tmpl, ctx);
        fail();
    } catch (MustacheException me) {} // expected
}