From ed9170c2c6a1b401d946be7db4936dcf7429dea1 Mon Sep 17 00:00:00 2001 From: Jeff Boulter Date: Fri, 5 Jan 2024 17:49:35 -0500 Subject: [PATCH] limit but do not throw an OutputTooBigException --- .../jinjava/el/TruthyTypeConverter.java | 8 +++- .../lib/exptest/IsEqualToExpTestTest.java | 46 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java b/src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java index caf11d391..74b163f23 100644 --- a/src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java +++ b/src/main/java/com/hubspot/jinjava/el/TruthyTypeConverter.java @@ -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; @@ -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) { @@ -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(); } diff --git a/src/test/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTestTest.java b/src/test/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTestTest.java index 695dadd71..fbe98aa63 100644 --- a/src/test/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTestTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/exptest/IsEqualToExpTestTest.java @@ -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 { @@ -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 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(