Skip to content

Commit

Permalink
Catch ELException in TypeConvertingMapELResolver to support map metho…
Browse files Browse the repository at this point in the history
…ds (#690)

* Catch ELException in TypeConvertingMapELResolver

* Add test
  • Loading branch information
liamrharwood authored Jun 17, 2021
1 parent bbf37df commit 7a17c69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit 7a17c69

Please sign in to comment.