Skip to content

Commit

Permalink
Try to parse params to numeric values
Browse files Browse the repository at this point in the history
Try to parse param access values to numeric
  • Loading branch information
GDLMadushanka committed Jan 26, 2025
1 parent f506d9f commit a0e513d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.apache.synapse.util.synapse.expression.context.EvaluationContext;
import org.apache.synapse.util.synapse.expression.exception.EvaluationException;

import java.math.BigDecimal;

/**
* Represents a node in the abstract syntax tree that provides access to headers and properties.
*/
Expand Down Expand Up @@ -80,11 +82,31 @@ public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValu
}
}
if (value != null) {
return new ExpressionResult(value.toString());
return tryParseNumber(value.toString());
} else {
throw new EvaluationException("Could not fetch the value of the key: " + name);
}
}
throw new EvaluationException("Key cannot be null when accessing headers or properties");
}


private ExpressionResult tryParseNumber(String value) {
try {
return new ExpressionResult(Integer.parseInt(value));
} catch (NumberFormatException e1) {
try {
return new ExpressionResult(Long.parseLong(value));
} catch (NumberFormatException e2) {
try {
Double.parseDouble(value);
// if double stored as big decimal, for accurate calculations
return new ExpressionResult(new BigDecimal(value));
} catch (NumberFormatException e3) {
return new ExpressionResult(value);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,12 @@ private ExpressionResult handleBase64EncodeFunction(ExpressionResult source, Exp
private ExpressionResult handleUrlEncodeFunction(ExpressionResult source, ExpressionResult argument1) {
if (source.isString() && argument1.isString()) {
try {
return new ExpressionResult(URLEncoder.encode(source.asString(), ExpressionUtils.getCharset(
argument1.asString())).replace("+", "%20").replace("*", "%2A"));
String result = URLEncoder.encode(source.asString(), ExpressionUtils.getCharset(argument1.asString()));
if (argument1.asString().equals("UTF-8")) {
return new ExpressionResult(result.replace("+", "%20").replace("*", "%2A"));
} else {
return new ExpressionResult(result);
}
} catch (UnsupportedCharsetException e) {
throw new EvaluationException("Invalid charset provided for urlEncode function. Charset: "
+ argument1.asString());
Expand Down

0 comments on commit a0e513d

Please sign in to comment.