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

Catch ELException in TypeConvertingMapELResolver to support map methods #690

Merged
merged 2 commits into from
Jun 17, 2021
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 @@ -2,6 +2,7 @@

import java.util.Map;
import javax.el.ELContext;
import javax.el.ELException;
import javax.el.MapELResolver;

public class TypeConvertingMapELResolver extends MapELResolver {
Expand All @@ -21,9 +22,13 @@ public Object getValue(ELContext context, Object base, Object property) {

if (base instanceof Map && !((Map) base).isEmpty()) {
Class<?> keyClass = ((Map) base).keySet().iterator().next().getClass();
value = ((Map) base).get(TYPE_CONVERTER.convert(property, keyClass));
if (value != null) {
context.setPropertyResolved(true);
try {
value = ((Map) base).get(TYPE_CONVERTER.convert(property, keyClass));
if (value != null) {
context.setPropertyResolved(true);
}
} catch (ELException ex) {
value = null;
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/hubspot/jinjava/el/ext/AstDictTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
import java.util.Map;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -36,6 +37,13 @@ public void itGetsDictValuesWithEnumKeysUsingToString() {
assertThat(interpreter.resolveELExpression("foo.barName", -1)).isEqualTo("test");
}

@Test
public void itDoesItemsMethodCall() {
interpreter.getContext().put("foo", ImmutableMap.of(TestEnum.BAR, "test"));
assertThat(interpreter.resolveELExpression("foo.items()", -1))
.isInstanceOf(Set.class);
}

@Test
public void itHandlesEmptyMaps() {
interpreter.getContext().put("foo", ImmutableMap.of());
Expand Down