From 836d2f0ed8caa806d5253add33ee8894ce13d9d6 Mon Sep 17 00:00:00 2001 From: Dulanjali Dilmi Date: Wed, 14 Feb 2024 15:41:29 +0530 Subject: [PATCH] Fix json path issue in Mediators Fix issue with Enrich mediator and foreach meadiator after Json Path Upgrade Fix https://github.com/wso2/micro-integrator/issues/3116 --- .../synapse/mediators/builtin/ForEachMediator.java | 12 ++++++++++-- .../apache/synapse/mediators/elementary/Target.java | 12 +++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java index 3483448b1d..dd3dc04d49 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ForEachMediator.java @@ -158,8 +158,16 @@ public boolean mediate(MessageContext synCtx) { if (((SynapseJsonPath) expression).isWholeBody()){ jsonPayloadElement = modifiedPayloadArray; } else { - jsonPayloadElement = parsedJsonPayload.set(((SynapseJsonPath) expression).getJsonPath(), - modifiedPayloadArray).json(); + Object list = ((SynapseJsonPath) expression).getJsonPath().read(jsonPayload); + if (!((JsonArray) list).isEmpty()) { + jsonPayloadElement = parsedJsonPayload.set(((SynapseJsonPath) expression).getJsonPath(), + modifiedPayloadArray).json(); + } else { + String jsonpathEp = ((SynapseJsonPath) expression).getJsonPathExpression(); + log.warn("No matching elements were found for the given JSONPath: " + jsonpathEp + ". Therefore, " + + "no elements were modified."); + jsonPayloadElement = parsedJsonPayload.json(); + } } try { JsonUtil.getNewJsonPayload(((Axis2MessageContext) synCtx).getAxis2MessageContext(), diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java index 1027629b42..b1b7ac4c54 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/elementary/Target.java @@ -527,9 +527,15 @@ private String removeJSONFromString(MessageContext synCtx, String inputString, S if (path.equals("$") || path.equals("$.")) { result = ""; } else { - DocumentContext doc = JsonPath.parse(result); - doc.delete(path); - result = doc.jsonString(); + Object list = JsonPath.compile(path).read(result); + if (!((JsonArray) list).isEmpty()) { + DocumentContext doc = JsonPath.parse(result); + doc.delete(path); + result = doc.jsonString(); + } else { + log.warn("No matching elements were found for the given JSONPath: " + path + ". Therefore, " + + "no elements were removed."); + } } } }