Skip to content

Commit

Permalink
Merge pull request #1142 from HubSpot/limit-coerced-strings
Browse files Browse the repository at this point in the history
limit coerced string length
  • Loading branch information
boulter authored Jan 8, 2024
2 parents f623369 + ed9170c commit 05e6579
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import de.odysseus.el.misc.TypeConverterImpl;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
import javax.el.ELException;

public class TruthyTypeConverter extends TypeConverterImpl {
private static final long serialVersionUID = 1L;
public static final int MAX_COLLECTION_STRING_LENGTH = 1_000_000;

@Override
protected Boolean coerceToBoolean(Object value) {
Expand Down Expand Up @@ -93,7 +96,44 @@ protected String coerceToString(Object value) {
if (value instanceof DummyObject) {
return "";
}
return super.coerceToString(value);
if (value == null) {
return "";
}
if (value instanceof String) {
return (String) value;
}
if (value instanceof Enum<?>) {
return ((Enum<?>) value).name();
}

if (value instanceof Collection) {
return coerceCollection((Collection) value);
}

return value.toString();
}

private String coerceCollection(Collection value) {
Iterator<?> it = value.iterator();
if (!it.hasNext()) {
return "[]";
}

StringBuilder sb = new StringBuilder();

sb.append('[');
for (;;) {
Object e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (sb.length() > MAX_COLLECTION_STRING_LENGTH) {
return sb.append(", ...]").toString();
}
if (!it.hasNext()) {
return sb.append(']').toString();
}
sb.append(',');
sb.append(' ');
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.hubspot.jinjava.BaseJinjavaTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.junit.Test;

public class IsEqualToExpTestTest extends BaseJinjavaTest {
Expand Down Expand Up @@ -35,6 +37,50 @@ public void itEquatesStrings() {
.isEqualTo("false");
}

@Test
public void itEquatesCollectionsToStrings() {
assertThat(
jinjava.render(
String.format(EQUAL_TEMPLATE, "\"[1, 2, 3]\"", "[1, 2, 3]"),
new HashMap<>()
)
)
.isEqualTo("true");

assertThat(
jinjava.render(
String.format(EQUAL_TEMPLATE, "\"[1, 2, 3]\"", "[1, 2, 4]"),
new HashMap<>()
)
)
.isEqualTo("false");
}

@Test
public void itEquatesLargeCollectionsAndStrings() {
assertThat(compareStringAndCollection(100_000)).isEqualTo("true");
}

@Test
public void itDoesNotEquateHugeCollectionsAndStrings() {
assertThat(compareStringAndCollection(500_000)).isEqualTo("false");
}

private String compareStringAndCollection(int size) {
List<Integer> bigList = new ArrayList<>();

for (int i = 0; i < size; i++) {
bigList.add(1);
}

String bigString = bigList.toString();

return jinjava.render(
String.format(EQUAL_TEMPLATE, "\"" + bigString + "\"", bigString),
new HashMap<>()
);
}

@Test
public void itEquatesBooleans() {
assertThat(
Expand Down

0 comments on commit 05e6579

Please sign in to comment.