Skip to content

Commit

Permalink
Null value in a Map should not throw AttributeNotFoundException, even…
Browse files Browse the repository at this point in the history
… with StrictVariables on. (#446) (#447)
  • Loading branch information
electrotype authored and ebussieres committed Jun 11, 2019
1 parent b01d0b4 commit 510c2df
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ public ResolvedAttribute resolve(Object instance,
return new ResolvedAttribute(null);
}

ResolvedAttribute resolvedAttribute;
Object key;
if (attributeNameValue != null && Number.class
.isAssignableFrom(attributeNameValue.getClass())) {
Number keyAsNumber = (Number) attributeNameValue;

Class<?> keyClass = object.keySet().iterator().next().getClass();
Object key = this.cast(keyAsNumber, keyClass, filename, lineNumber);
resolvedAttribute = new ResolvedAttribute(object.get(key));
key = this.cast(keyAsNumber, keyClass, filename, lineNumber);
} else {
resolvedAttribute = new ResolvedAttribute(object.get(attributeNameValue));
key = attributeNameValue;
}

if (context.isStrictVariables() && resolvedAttribute.evaluatedValue == null) {
if(context.isStrictVariables() && !object.containsKey(key)) {
throw new AttributeNotFoundException(null, String.format(
"Attribute [%s] of [%s] does not exist or can not be accessed and strict variables is set to true.",
attributeNameValue.toString(), object.getClass().getName()),
attributeNameValue.toString(), lineNumber, filename);
}

ResolvedAttribute resolvedAttribute = new ResolvedAttribute(object.get(key));
return resolvedAttribute;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@ public void testNonExistingMapAttributeWithStrictVariables() throws PebbleExcept
template.evaluate(writer, context);
}

@Test
public void testNullMapValueWithoutStrictVariables() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
.strictVariables(false).build();

PebbleTemplate template = pebble.getTemplate("hello {{ map.name }}");
Map<String, Object> context = new HashMap<>();

Map<String, Object> map = new HashMap<>();
map.put("name", null);
context.put("map", map);

Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("hello ", writer.toString());
}

/**
* Issue 446
*/
@Test
public void testNullMapValueWithStrictVariables() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
.strictVariables(true).build();

PebbleTemplate template = pebble.getTemplate("hello {{ map.name }}");
Map<String, Object> context = new HashMap<>();

Map<String, Object> map = new HashMap<>();
map.put("name", null);
context.put("map", map);

Writer writer = new StringWriter();
template.evaluate(writer, context);
assertEquals("hello ", writer.toString());
}

@Test
public void testMethodAttribute() throws PebbleException, IOException {
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
Expand Down

0 comments on commit 510c2df

Please sign in to comment.