Skip to content

Commit

Permalink
Merge pull request #314 from HubSpot/fix-in
Browse files Browse the repository at this point in the history
Use type converter when evaulting 'in'
  • Loading branch information
mattcoley authored Mar 25, 2019
2 parents fb701de + aad3611 commit 1d475ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Collection;
import java.util.Objects;

import javax.el.ELException;

import org.apache.commons.lang3.StringUtils;

import de.odysseus.el.misc.TypeConverter;
Expand All @@ -26,7 +28,21 @@ protected Object apply(TypeConverter converter, Object o1, Object o2) {
}

if (Collection.class.isAssignableFrom(o2.getClass())) {
return ((Collection<?>) o2).contains(o1);
Collection<?> collection = (Collection<?>) o2;

for (Object value : collection) {
if (value == null) {
if (o1 == null) {
return Boolean.TRUE;
}
} else {
try {
return collection.contains(converter.convert(o1, value.getClass()));
} catch (ELException e) {
return Boolean.FALSE;
}
}
}
}

return Boolean.FALSE;
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/com/hubspot/jinjava/lib/filter/IntFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.time.ZoneOffset;
import java.util.Locale;

import org.apache.commons.lang3.math.NumberUtils;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -113,4 +112,14 @@ public void itInterpretsFrenchCommasAndPeriodsWithFrenchLocale() {
interpreter = new Jinjava(FRENCH_LOCALE_CONFIG).newInterpreter();
assertThat(filter.filter("123\u00A0123,12", interpreter)).isEqualTo(123123);
}

@Test
public void itConvertsProperlyInExpressionTest() {
assertThat(interpreter.render("{{ '3'|int in [null, 4, 5, 6, null, 3] }}")).isEqualTo("true");
}

@Test
public void itConvertsProperlyInExpressionTestWithWrongType() {
assertThat(interpreter.render("{{ 'test' in [null, 4, 5, 6, null, 3] }}")).isEqualTo("false");
}
}

0 comments on commit 1d475ba

Please sign in to comment.