Skip to content

Commit

Permalink
limit but do not throw an OutputTooBigException
Browse files Browse the repository at this point in the history
  • Loading branch information
jboulter committed Jan 5, 2024
1 parent 95330ce commit ed9170c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.hubspot.jinjava.el;

import com.hubspot.jinjava.objects.DummyObject;
import com.hubspot.jinjava.util.LengthLimitingStringBuilder;
import com.hubspot.jinjava.util.ObjectTruthValue;
import de.odysseus.el.misc.TypeConverterImpl;
import java.math.BigDecimal;
Expand All @@ -13,6 +12,7 @@

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 @@ -119,11 +119,15 @@ private String coerceCollection(Collection value) {
return "[]";
}

LengthLimitingStringBuilder sb = new LengthLimitingStringBuilder(1_000_000L);
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();
}
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 ed9170c

Please sign in to comment.