diff --git a/pebble/src/main/java/com/mitchellbosecke/pebble/attributes/MapResolver.java b/pebble/src/main/java/com/mitchellbosecke/pebble/attributes/MapResolver.java index 84512df5a..b1c33e871 100644 --- a/pebble/src/main/java/com/mitchellbosecke/pebble/attributes/MapResolver.java +++ b/pebble/src/main/java/com/mitchellbosecke/pebble/attributes/MapResolver.java @@ -4,6 +4,7 @@ import com.mitchellbosecke.pebble.error.PebbleException; import com.mitchellbosecke.pebble.node.ArgumentsNode; import com.mitchellbosecke.pebble.template.EvaluationContextImpl; + import java.util.Map; class MapResolver implements AttributeResolver { @@ -22,7 +23,7 @@ public ResolvedAttribute resolve(Object instance, String filename, int lineNumber) { Map object = (Map) instance; - if (object.isEmpty()) { + if (object.isEmpty() && !context.isStrictVariables()) { return new ResolvedAttribute(null); } diff --git a/pebble/src/test/java/com/mitchellbosecke/pebble/GetAttributeTest.java b/pebble/src/test/java/com/mitchellbosecke/pebble/GetAttributeTest.java index e82525d4f..ce2e338e9 100644 --- a/pebble/src/test/java/com/mitchellbosecke/pebble/GetAttributeTest.java +++ b/pebble/src/test/java/com/mitchellbosecke/pebble/GetAttributeTest.java @@ -142,6 +142,23 @@ void testNonExistingMapAttributeWithStrictVariables() throws PebbleException, IO }); } + @Test + void testNonExistingMapAttributeWithStrictVariablesAndEmptyMap() throws PebbleException, IOException { + assertThrows(AttributeNotFoundException.class, () -> { + PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()) + .strictVariables(true).build(); + + String source = "{{ object.nonExisting }}"; + PebbleTemplate template = pebble.getTemplate(source); + + Map context = new HashMap<>(); + context.put("object", new HashMap<>()); + + Writer writer = new StringWriter(); + template.evaluate(writer, context); + }); + } + @Test void testNullMapValueWithoutStrictVariables() throws PebbleException, IOException { PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())