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

Use type converter when evaulting 'in' #314

Merged
merged 6 commits into from
Mar 25, 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 @@ -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");
}
}