From 33e8b438be6ec1c923b435076f1e64c6017f1f89 Mon Sep 17 00:00:00 2001 From: Eric Bussieres Date: Wed, 18 Dec 2019 14:14:30 -0500 Subject: [PATCH] Empty map in strict mode does not throw exception (#491) (#492) --- .../pebble/attributes/MapResolver.java | 3 ++- .../pebble/GetAttributeTest.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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())