Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null value in a Map should not throw AttributeNotFoundException #447

Merged
merged 1 commit into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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