Skip to content

Commit

Permalink
Empty map in strict mode does not throw exception (#491) (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebussieres authored Dec 18, 2019
1 parent ca9ebdc commit 33e8b43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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())
Expand Down

0 comments on commit 33e8b43

Please sign in to comment.