Skip to content

Commit

Permalink
Fix registry property access
Browse files Browse the repository at this point in the history
Fix registry property access and few other issues.
Related to wso2/product-micro-integrator/issues/3749
  • Loading branch information
GDLMadushanka committed Dec 3, 2024
1 parent 630fa9a commit 63fff63
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
Expand All @@ -37,7 +35,6 @@
import org.apache.synapse.util.synapse.expression.context.EvaluationContext;
import org.apache.synapse.util.synapse.expression.exception.EvaluationException;
import org.apache.synapse.util.synapse.expression.utils.ExpressionUtils;
import org.apache.synapse.util.xpath.SynapseJsonPath;
import org.jaxen.JaxenException;

import java.io.IOException;
Expand Down Expand Up @@ -128,7 +125,7 @@ public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValu
String expressionToEvaluate = variableAndExpression[1];
// if no expression just return variable
if (StringUtils.isEmpty(expressionToEvaluate)) {
return parseVariable(variable);
result = variable;
} else {
expressionToEvaluate = expressionToEvaluate.startsWith(".") ? "$" + expressionToEvaluate
: "$." + expressionToEvaluate;
Expand Down Expand Up @@ -193,6 +190,12 @@ public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValu
return new ExpressionResult((Boolean) result);
} else if (result instanceof List) {
return new ExpressionResult((List) result);
} else if (result instanceof Float) {
return new ExpressionResult((Double) result);
} else if (result instanceof Short) {
return new ExpressionResult((Integer) result);
} else if (result instanceof OMNode) {
return new ExpressionResult((OMNode) result);
}
return null;
}
Expand All @@ -208,36 +211,4 @@ private void processResult(Map.Entry<String, ExpressionNode> entry, ExpressionRe
}
expression = expression.replaceFirst(regex, resultString);
}

private ExpressionResult parseVariable(Object variable) {
if (variable instanceof List) {
return new ExpressionResult((List) variable);
} else if (variable instanceof OMNode) {
return new ExpressionResult((OMNode) variable);
} else if (variable instanceof JsonElement) {
return new ExpressionResult((JsonElement) variable);
} else {
try {
return new ExpressionResult(Integer.parseInt(variable.toString()));
} catch (NumberFormatException e1) {
// If integer parsing fails, attempt to parse as double
try {
return new ExpressionResult(Double.parseDouble(variable.toString()));
} catch (NumberFormatException e2) {
// If double parsing fails, attempt to parse as JSON
try {
if (variable.equals("")) {
// avoid converting empty string to NULL value
return new ExpressionResult("");
}
JsonElement jsonElement = JsonParser.parseString(variable.toString());
return new ExpressionResult(jsonElement);
} catch (JsonSyntaxException e3) {
// If JSON parsing fails, return the variable as a string
return new ExpressionResult(variable.toString());
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class ExpressionConstants {
public static final String OBJECT = "object";
public static final String ARRAY = "array";
public static final String REGISTRY = "registry";
public static final String PROPERTY = "property";
public static final String EXISTS = "exists";
public static final String XPATH = "xpath";
public static final String SECRET = "secret";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@ public ExpressionNode visitFunctionCall(ExpressionParser.FunctionCallContext ctx
return new PredefinedFunctionNode(parameterList, ExpressionConstants.NOT);
case ExpressionConstants.REGISTRY:
if (ctx.functionCallSuffix() != null) {
if (ctx.functionCallSuffix().jsonPathExpression() != null) {
if (ctx.functionCallSuffix().ID() != null &&
ExpressionConstants.PROPERTY.equals(ctx.functionCallSuffix().ID().getText()) &&
!ctx.functionCallSuffix().expression().isEmpty()) {
parameterList.addArgument(visit(ctx.functionCallSuffix().expression(0)));
return new PredefinedFunctionNode(parameterList, ExpressionConstants.REGISTRY);
} else if (ctx.functionCallSuffix().jsonPathExpression() != null) {
PredefinedFunctionNode node = new PredefinedFunctionNode(parameterList,
ExpressionConstants.REGISTRY);
return visitJsonPathAfterPayload(ctx.functionCallSuffix().jsonPathExpression(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public SynapseExpression(String synapseExpression) throws JaxenException {
while (matcher.find()) {
if (matcher.group(2) != null) {
// evaluating xpath on a variable so not content aware
isContentAware = false;
break;
continue;
}
String xpath = matcher.group(1);
try {
Expand Down

0 comments on commit 63fff63

Please sign in to comment.