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

Update expression resolver to return null instead of blank string. #296

Merged
merged 3 commits into from
Mar 6, 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
4 changes: 2 additions & 2 deletions src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ExpressionResolver(JinjavaInterpreter interpreter, ExpressionFactory expr
*/
public Object resolveExpression(String expression) {
if (StringUtils.isBlank(expression)) {
return "";
return null;
}

interpreter.getContext().addResolvedExpression(expression.trim());
Expand Down Expand Up @@ -121,7 +121,7 @@ else if (e.getCause() != null && e.getCause() instanceof InvalidArgumentExceptio
String.format("Error resolving expression [%s]: " + getRootCauseMessage(e), expression), e, interpreter.getLineNumber(), interpreter.getPosition())));
}

return "";
return null;
}

private void validateResult(Object result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ public void itBlocksDisabledFunctions() {
final JinjavaConfig config = JinjavaConfig.newBuilder().withDisabled(disabled).build();

final RenderResult renderResult = jinjava.renderForResult(template, context, config);
assertEquals("hi ", renderResult.getOutput());
assertEquals("hi ", renderResult.getOutput());
TemplateError e = renderResult.getErrors().get(0);
assertThat(e.getItem()).isEqualTo(ErrorItem.FUNCTION);
assertThat(e.getReason()).isEqualTo(ErrorReason.DISABLED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ public void arrayWithNegativeIndices() {
assertThat(val("stringToSplit.split('-')")).isEqualTo(new String[]{ "one", "two", "three", "four", "five" });

assertThat(val("stringToSplit.split('-')[-1]")).isEqualTo("five");
assertThat(val("stringToSplit.split('-')[1.5]")).isEqualTo("");
assertThat(val("stringToSplit.split('-')[-1.5]")).isEqualTo("");
assertThat(val("stringToSplit.split('-')[1.5]")).isEqualTo(null);
assertThat(val("stringToSplit.split('-')[-1.5]")).isEqualTo(null);

// out of range returns null, as -6 + the length of the array is still
// negative, and java doesn't support negative array indices.
Expand All @@ -287,23 +287,23 @@ public void arrayWithNegativeIndices() {

@Test
public void invalidNestedAssignmentExpr() {
assertThat(val("content.template_path = 'Custom/Email/Responsive/testing.html'")).isEqualTo("");
assertThat(val("content.template_path = 'Custom/Email/Responsive/testing.html'")).isEqualTo(null);
assertThat(interpreter.getErrorsCopy()).isNotEmpty();
assertThat(interpreter.getErrorsCopy().get(0).getReason()).isEqualTo(ErrorReason.SYNTAX_ERROR);
assertThat(interpreter.getErrorsCopy().get(0).getMessage()).containsIgnoringCase("identifier");
}

@Test
public void invalidIdentifierAssignmentExpr() {
assertThat(val("content = 'Custom/Email/Responsive/testing.html'")).isEqualTo("");
assertThat(val("content = 'Custom/Email/Responsive/testing.html'")).isEqualTo(null);
assertThat(interpreter.getErrorsCopy()).isNotEmpty();
assertThat(interpreter.getErrorsCopy().get(0).getReason()).isEqualTo(ErrorReason.SYNTAX_ERROR);
assertThat(interpreter.getErrorsCopy().get(0).getMessage()).containsIgnoringCase("'='");
}

@Test
public void invalidPipeOperatorExpr() {
assertThat(val("topics|1")).isEqualTo("");
assertThat(val("topics|1")).isEqualTo(null);
assertThat(interpreter.getErrorsCopy()).isNotEmpty();
assertThat(interpreter.getErrorsCopy().get(0).getReason()).isEqualTo(ErrorReason.SYNTAX_ERROR);
}
Expand Down