You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Harish Annamalai (Jira)" <ji...@apache.org> on 2024/08/22 10:48:00 UTC
[jira] [Created] (CAMEL-21109) Choice evaluation behaves inconsistently when source is String and Value is Float.
Harish Annamalai created CAMEL-21109:
----------------------------------------
Summary: Choice evaluation behaves inconsistently when source is String and Value is Float.
Key: CAMEL-21109
URL: https://issues.apache.org/jira/browse/CAMEL-21109
Project: Camel
Issue Type: Bug
Components: camel-core
Affects Versions: 3.x
Reporter: Harish Annamalai
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|https://github.com/apache/camel/blob/8e512e50a49f309ae2f9641f59dbfe561e7c0a13/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java#L147C1-L161C10]
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) {
return leftNum.compareTo(rightNum);
}
// 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) {
return leftDouble.compareTo(rightDouble);
}
*Camel 3x Code:*
[ObjectHelper.java|https://github.com/apache/camel/blob/bb339e9eb26fa38b448d74bbb058c70d0a057f33/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java#L261]
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)) {
// favour to use numeric comparison
Long num1 = Long.parseLong(leftNum);
Long num2 = Long.parseLong(rightNum);
return num1.compareTo(num2);
}
return leftNum.compareTo(rightNum);
} else if (leftValue instanceof Integer && rightValue instanceof Integer) {
--
This message was sent by Atlassian Jira
(v8.20.10#820010)