From 4e29a11846ccd1976afbf53c2f84a221c9e7e31b Mon Sep 17 00:00:00 2001 From: Liam Harwood Date: Thu, 17 Jun 2021 15:26:55 -0400 Subject: [PATCH 1/2] Catch ELException in TypeConvertingMapELResolver --- .../jinjava/el/TypeConvertingMapELResolver.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/el/TypeConvertingMapELResolver.java b/src/main/java/com/hubspot/jinjava/el/TypeConvertingMapELResolver.java index 73de933cc..7d90543e3 100644 --- a/src/main/java/com/hubspot/jinjava/el/TypeConvertingMapELResolver.java +++ b/src/main/java/com/hubspot/jinjava/el/TypeConvertingMapELResolver.java @@ -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 { @@ -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; } } From 4dfa3af21a16dba2f1ea904f94d60b0222443173 Mon Sep 17 00:00:00 2001 From: Liam Harwood Date: Thu, 17 Jun 2021 15:30:49 -0400 Subject: [PATCH 2/2] Add test --- src/test/java/com/hubspot/jinjava/el/ext/AstDictTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/com/hubspot/jinjava/el/ext/AstDictTest.java b/src/test/java/com/hubspot/jinjava/el/ext/AstDictTest.java index d096eb342..7e60db6b5 100644 --- a/src/test/java/com/hubspot/jinjava/el/ext/AstDictTest.java +++ b/src/test/java/com/hubspot/jinjava/el/ext/AstDictTest.java @@ -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; @@ -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());