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

Change to not throw exceptions for number to string comparisons when not in strict mode #305

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package liqp.exceptions;

public class IncompatibleTypeComparisonException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final Object a;
private final Object b;

public IncompatibleTypeComparisonException(Object a, Object b) {
super();
this.a = a;
this.b = b;
}

@Override
public String getMessage() {
String aType = a == null ? "null" : a.getClass().getName();
String bType = b == null ? "null" : b.getClass().getName();
return "Cannot compare " + a + " with " + b + " because they are not the same type: " + aType + " vs " + bType;
}
}
10 changes: 7 additions & 3 deletions src/main/java/liqp/nodes/GtEqNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package liqp.nodes;

import liqp.exceptions.IncompatibleTypeComparisonException;

public class GtEqNode extends ComparingExpressionNode {

public GtEqNode(LNode lhs, LNode rhs) {
Expand All @@ -14,8 +16,10 @@ Object doCompare(Object a, Object b, boolean strictTypedExpressions) {
} else if (b instanceof Comparable && b.getClass().isInstance(a)) {
return ((Comparable<Object>) b).compareTo(a) < 0;
}
String aType = a == null ? "null" : a.getClass().getName();
String bType = b == null ? "null" : b.getClass().getName();
throw new RuntimeException("Cannot compare " + a + " with " + b + " because they are not the same type: " + aType + " vs " + bType);

if (strictTypedExpressions) {
throw new IncompatibleTypeComparisonException(a, b);
}
return false;
}
}
9 changes: 6 additions & 3 deletions src/main/java/liqp/nodes/GtNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package liqp.nodes;

import liqp.exceptions.IncompatibleTypeComparisonException;

import java.util.Optional;

public class GtNode extends ComparingExpressionNode {
Expand All @@ -18,9 +20,10 @@ Object doCompare(Object a, Object b, boolean strictTypedExpressions) {
return ((Comparable<Object>) b).compareTo(a) <= 0;
}

String aType = a == null ? "null" : a.getClass().getName();
String bType = b == null ? "null" : b.getClass().getName();
throw new RuntimeException("Cannot compare " + a + " with " + b + " because they are not the same type: " + aType + " vs " + bType);
if (strictTypedExpressions) {
throw new IncompatibleTypeComparisonException(a, b);
}
return false;
}

}
9 changes: 6 additions & 3 deletions src/main/java/liqp/nodes/LtEqNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package liqp.nodes;

import liqp.exceptions.IncompatibleTypeComparisonException;

import java.util.Optional;

public class LtEqNode extends ComparingExpressionNode {
Expand All @@ -17,8 +19,9 @@ Object doCompare(Object a, Object b, boolean strictTypedExpressions) {
return ((Comparable<Object>) b).compareTo(a) > 0;
}

String aType = a == null ? "null" : a.getClass().getName();
String bType = b == null ? "null" : b.getClass().getName();
throw new RuntimeException("Cannot compare " + a + " with " + b + " because they are not the same type: " + aType + " vs " + bType);
if (strictTypedExpressions) {
throw new IncompatibleTypeComparisonException(a, b);
}
return false;
}
}
10 changes: 7 additions & 3 deletions src/main/java/liqp/nodes/LtNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package liqp.nodes;

import liqp.exceptions.IncompatibleTypeComparisonException;

import java.util.Optional;

public class LtNode extends ComparingExpressionNode {
Expand All @@ -17,8 +19,10 @@ Object doCompare(Object a, Object b, boolean strictTypedExpressions) {
} else if (b instanceof Comparable && b.getClass().isInstance(a)) {
return ((Comparable<Object>) b).compareTo(a) >= 0;
}
String aType = a == null ? "null" : a.getClass().getName();
String bType = b == null ? "null" : b.getClass().getName();
throw new RuntimeException("Cannot compare " + a + " with " + b + " because they are not the same type: " + aType + " vs " + bType);

if (strictTypedExpressions) {
throw new IncompatibleTypeComparisonException(a, b);
}
return false;
}
}
14 changes: 14 additions & 0 deletions src/test/java/liqp/nodes/GtEqNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,18 @@ public void testComparableTypes() {
value = TemplateParser.DEFAULT.parse("{% if a >= c %}yes{% else %}no{% endif %}").render(data);
assertEquals("no", value);
}

@Test
public void testStrictModeDisabled() {
String[][] tests = {
{"{% if 0 >= 'A' %}yes{% else %}no{% endif %}", "no"},
{"{% if 'A' >= 0 %}yes{% else %}no{% endif %}", "no"},
{"{% if false >= 1 %}yes{% else %}no{% endif %}", "no"},
};
TemplateParser templateParser = new TemplateParser.Builder().withStrictTypedExpressions(false).build();
for (String[] test : tests) {
String rendered = templateParser.parse(test[0]).render();
assertThat(rendered, is(test[1]));
}
}
}
14 changes: 14 additions & 0 deletions src/test/java/liqp/nodes/GtNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,18 @@ public void testFilterCompare() {
.render();
assertTrue(Boolean.parseBoolean(result));
}

@Test
public void testStrictModeDisabled() {
String[][] tests = {
{"{% if 0 > 'A' %}yes{% else %}no{% endif %}", "no"},
{"{% if 'A' > 0 %}yes{% else %}no{% endif %}", "no"},
{"{% if false > 1 %}yes{% else %}no{% endif %}", "no"},
};
TemplateParser templateParser = new TemplateParser.Builder().withStrictTypedExpressions(false).build();
for (String[] test : tests) {
String rendered = templateParser.parse(test[0]).render();
assertThat(rendered, is(test[1]));
}
}
}
14 changes: 14 additions & 0 deletions src/test/java/liqp/nodes/LtEqNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,18 @@ public void testComparableTypes() {
value = TemplateParser.DEFAULT.parse("{% if a <= c %}yes{% else %}no{% endif %}").render(data);
assertEquals("yes", value);
}

@Test
public void testStrictModeDisabled() {
String[][] tests = {
{"{% if 0 <= 'A' %}yes{% else %}no{% endif %}", "no"},
{"{% if 'A' <= 0 %}yes{% else %}no{% endif %}", "no"},
{"{% if true <= 0 %}yes{% else %}no{% endif %}", "no"},
};
TemplateParser templateParser = new TemplateParser.Builder().withStrictTypedExpressions(false).build();
for (String[] test : tests) {
String rendered = templateParser.parse(test[0]).render();
assertThat(rendered, is(test[1]));
}
}
}
14 changes: 14 additions & 0 deletions src/test/java/liqp/nodes/LtNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,18 @@ public void testComparableTypes() {
value = TemplateParser.DEFAULT.parse("{% if a < c %}yes{% else %}no{% endif %}").render(data);
assertEquals("yes", value);
}

@Test
public void testStrictModeDisabled() {
String[][] tests = {
{"{% if 0 < 'A' %}yes{% else %}no{% endif %}", "no"},
{"{% if 'A' < 0 %}yes{% else %}no{% endif %}", "no"},
{"{% if true < 0 %}yes{% else %}no{% endif %}", "no"},
};
TemplateParser templateParser = new TemplateParser.Builder().withStrictTypedExpressions(false).build();
for (String[] test : tests) {
String rendered = templateParser.parse(test[0]).render();
assertThat(rendered, is(test[1]));
}
}
}
Loading