Skip to content

Commit 77c669d

Browse files
committed
Check versus max output size because max string length should just be used for truncating
1 parent f3fd619 commit 77c669d

5 files changed

Lines changed: 10 additions & 9 deletions

File tree

src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public JinjavaInterpreter(JinjavaInterpreter orig) {
126126
scopeDepth = orig.getScopeDepth() + 1;
127127
}
128128

129-
public static void checkStringLength(String string) {
129+
public static void checkOutputSize(String string) {
130130
Optional<Long> maxStringLength = getCurrentMaybe()
131-
.map(interpreter -> interpreter.getConfig().getMaxStringLength())
131+
.map(interpreter -> interpreter.getConfig().getMaxOutputSize())
132132
.filter(max -> max > 0);
133133
if (maxStringLength.map(max -> string.length() > max).orElse(false)) {
134134
throw new OutputTooBigException(maxStringLength.get(), string.length());

src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerTagDecorator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public T getTag() {
3636
public final String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
3737
try {
3838
String output = innerInterpret(tagNode, interpreter);
39-
JinjavaInterpreter.checkStringLength(output);
39+
JinjavaInterpreter.checkOutputSize(output);
4040
return output;
4141
} catch (DeferredValueException | TemplateSyntaxException | OutputTooBigException e) {
4242
try {

src/main/java/com/hubspot/jinjava/objects/serialization/PyishObjectMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static String getAsPyishStringOrThrow(Object val)
5050
throws JsonProcessingException {
5151
String string = PYISH_OBJECT_WRITER.writeValueAsString(val);
5252
Optional<JinjavaInterpreter> interpreterMaybe = JinjavaInterpreter.getCurrentMaybe();
53-
JinjavaInterpreter.checkStringLength(string);
53+
JinjavaInterpreter.checkOutputSize(string);
5454
if (
5555
interpreterMaybe
5656
.map(

src/main/java/com/hubspot/jinjava/util/EagerExpressionResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.hubspot.jinjava.el.ext.ExtendedParser;
88
import com.hubspot.jinjava.interpret.DeferredValueException;
99
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
10+
import com.hubspot.jinjava.interpret.OutputTooBigException;
1011
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
1112
import com.hubspot.jinjava.interpret.UnknownTokenException;
1213
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
@@ -107,7 +108,7 @@ public static String getValueAsJinjavaStringSafe(Object val) {
107108
return pyishString;
108109
}
109110
}
110-
} catch (JsonProcessingException ignored) {}
111+
} catch (JsonProcessingException | OutputTooBigException ignored) {}
111112
throw new DeferredValueException("Can not convert deferred result to string");
112113
}
113114

src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerForTagTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ public void itHandlesNestedDeferredForLoop() {
8787

8888
@Test
8989
public void itLimitsLength() {
90-
interpreter.render(
90+
String out = interpreter.render(
9191
String.format(
9292
"{%% for item in (range(1000, %s)) + deferred %%}{%% endfor %%}",
9393
MAX_OUTPUT_SIZE
9494
)
9595
);
96-
assertThat(interpreter.getContext().getDeferredNodes()).hasSize(1);
96+
assertThat(interpreter.getContext().getDeferredTokens()).hasSize(1);
9797
}
9898

9999
@Test
@@ -102,11 +102,11 @@ public void itUsesDeferredExecutionModeWhenChildrenAreLarge() {
102102
interpreter.render(
103103
String.format(
104104
"{%% for item in range(%d) %%}1234567890{%% endfor %%}",
105-
MAX_OUTPUT_SIZE / 10
105+
MAX_OUTPUT_SIZE / 10 - 1
106106
)
107107
)
108108
)
109-
.hasSize((int) MAX_OUTPUT_SIZE);
109+
.hasSize((int) MAX_OUTPUT_SIZE - 10);
110110
assertThat(interpreter.getContext().getDeferredTokens()).isEmpty();
111111
assertThat(interpreter.getContext().getDeferredNodes()).isEmpty();
112112
assertThat(interpreter.getErrors()).isEmpty();

0 commit comments

Comments
 (0)