Details
-
Bug
-
Status: Resolved
-
Minor
-
Resolution: Fixed
-
3.x
-
None
-
Unknown
Description
Choice evaluation works inconsistently if the condition contains Predicates with String, but the value is float.
Consider the choice expression:
${exchangeProperty.left} >= ${exchangeProperty.right}
If the both the properties are 'left' and 'right', but value contained in them is float like '4.0' '7.5' etc, the expression is compared as Strings and returns false. However, if the value contained in them is "Pure" Integer, then they are compared as "Numbers" and Comparision returns true.
Please see the Examples:
Example 1:
from("timer:getter?period=1000&repeatCount=1")
.setProperty("left", simple("40"))
.setProperty("right", simple("7.5"))
.choice()
.when(simple("${exchangeProperty.left} >= ${exchangeProperty.right}"))
.to("log://match")
.otherwise()
.to("log://otherwise")
.end();
The Predicate in Example 1, evaluation returns false for the case above as it compares 40 and 7.5 as Strings.
Example 2:
from("timer:getter?period=1000&repeatCount=1")
.setProperty("left", simple("40"))
.setProperty("right", simple("7"))
.choice()
.when(simple("${exchangeProperty.left} >= ${exchangeProperty.right}"))
.to("log://match")
.otherwise()
.to("log://otherwise")
.end();
The Predicate in Example 2, evaluation returns true for the case above as it compares 40 and 7 as Numbers.
We debugged the Camel Code, and compared the behavior against Camel 2x, we can see that the predicate evaluation takes "Double" also into consideration.
Camel 2x Code:
ObjectHelper.java
public static int typeCoerceCompare(TypeConverter converter, Object leftValue, Object rightValue) {
// if both values is numeric then compare using numeric
Long leftNum = converter.tryConvertTo(Long.class, leftValue);
Long rightNum = converter.tryConvertTo(Long.class, rightValue);
if (leftNum != null && rightNum != null)
// also try with floating point numbers
Double leftDouble = converter.tryConvertTo(Double.class, leftValue);
Double rightDouble = converter.tryConvertTo(Double.class, rightValue);
if (leftDouble != null && rightDouble != null)
Camel 3x Code:
ObjectHelper.java
public static int typeCoerceCompare(TypeConverter converter, Object leftValue, Object rightValue) {
// optimize for common combinations of comparing numbers
if (leftValue instanceof String && rightValue instanceof String) {
String leftNum = (String) leftValue;
String rightNum = (String) rightValue;
if (isNumber(leftNum) && isNumber(rightNum))
return leftNum.compareTo(rightNum);
} else if (leftValue instanceof Integer && rightValue instanceof Integer) {
Attachments
Issue Links
- links to