Skip to content

Commit

Permalink
dont throw expcetions for number to string comparisons when not in st…
Browse files Browse the repository at this point in the history
…rict mode (#305)
  • Loading branch information
Hunter-Gong-Attentive authored Jul 9, 2024
1 parent 0132078 commit 0fb9061
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 12 deletions.
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]));
}
}
}

0 comments on commit 0fb9061

Please sign in to comment.