From f0a9cbac4e3ffe631227ed6bd6e8da1b787ceb37 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Tue, 8 Oct 2024 16:24:19 +0200 Subject: [PATCH 1/3] [Fix #3709] Allow cloning when source node is the merge result --- .../workflow/actions/InjectAction.java | 2 +- .../kie/kogito/jackson/utils/MergeUtils.java | 9 ++++++- .../kogito/jackson/utils/MergeUtilsTest.java | 25 +++++++++++++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java index e67a2f85b37..54c73966691 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java @@ -51,6 +51,6 @@ private static JsonNode readObject(String json) { @Override public void execute(KogitoProcessContext context) throws Exception { - context.setVariable(SWFConstants.DEFAULT_WORKFLOW_VAR, MergeUtils.merge(node, getWorkflowData(context))); + context.setVariable(SWFConstants.DEFAULT_WORKFLOW_VAR, MergeUtils.merge(node, getWorkflowData(context), false, true)); } } diff --git a/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/MergeUtils.java b/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/MergeUtils.java index 1ddcf12c8c0..6da6b9c9539 100644 --- a/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/MergeUtils.java +++ b/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/MergeUtils.java @@ -39,13 +39,20 @@ public static JsonNode merge(JsonNode src, JsonNode target) { } public static JsonNode merge(JsonNode src, JsonNode target, boolean mergeArray) { + return merge(src, target, mergeArray, false); + } + + public static JsonNode merge(JsonNode src, JsonNode target, boolean mergeArray, boolean cloneSource) { if (target == null || target.isNull() || target.isObject() && target.isEmpty() && src != null && !src.isNull()) { - return src; + return cloneSource ? src.deepCopy() : src; } else if (target.isArray()) { return mergeArray(src, (ArrayNode) target, mergeArray); } else if (target.isObject()) { return mergeObject(src, (ObjectNode) target, mergeArray); } else { + if (cloneSource) { + src = src.deepCopy(); + } if (src.isArray()) { ArrayNode srcArray = (ArrayNode) src; insert(srcArray, target, getExistingNodes(srcArray)); diff --git a/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/MergeUtilsTest.java b/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/MergeUtilsTest.java index c7f034393ed..64df78d3e76 100644 --- a/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/MergeUtilsTest.java +++ b/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/MergeUtilsTest.java @@ -47,7 +47,18 @@ void testTargetArraySrcInt() { @Test void testTargetIntSrcArray() { - assertThat(JsonObjectUtils.toJavaValue(MergeUtils.merge(ObjectMapperFactory.get().createArrayNode().add(1).add(2).add(3), new IntNode(4), false))).isEqualTo(Arrays.asList(4, 1, 2, 3)); + ArrayNode src = ObjectMapperFactory.get().createArrayNode().add(1).add(2).add(3); + JsonNode result = MergeUtils.merge(src, new IntNode(4), false); + assertThat(src).isSameAs(result); + assertThat(JsonObjectUtils.toJavaValue(result)).isEqualTo(Arrays.asList(4, 1, 2, 3)); + } + + @Test + void testTargetIntSrcArrayClone() { + ArrayNode src = ObjectMapperFactory.get().createArrayNode().add(1).add(2).add(3); + JsonNode result = MergeUtils.merge(src, new IntNode(4), false, true); + assertThat(src).isNotSameAs(result); + assertThat(JsonObjectUtils.toJavaValue(result)).isEqualTo(Arrays.asList(4, 1, 2, 3)); } @Test @@ -109,7 +120,17 @@ void testObjectArrayMergeNoDuplicate() { @Test void testNullMerge() { JsonNode srcNode = ObjectMapperFactory.get().createObjectNode().put("name", "javierito"); - assertThat(MergeUtils.merge(srcNode, null)).isEqualTo(srcNode); + JsonNode result = MergeUtils.merge(srcNode, null); + assertThat(result).isSameAs(srcNode); + + } + + @Test + void testNullMergeClone() { + JsonNode srcNode = ObjectMapperFactory.get().createObjectNode().put("name", "javierito"); + JsonNode result = MergeUtils.merge(srcNode, null, false, true); + assertThat(result).isNotSameAs(srcNode); + assertThat(result).isEqualTo(srcNode); } private static class Person { From fe1f4a5e787527317dadd9fdb502a5d099c1a952 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Tue, 8 Oct 2024 16:32:51 +0200 Subject: [PATCH 2/3] Revert "[Fix #3709] Allow cloning when source node is the merge result" This reverts commit f0a9cbac4e3ffe631227ed6bd6e8da1b787ceb37. --- .../workflow/actions/InjectAction.java | 2 +- .../kie/kogito/jackson/utils/MergeUtils.java | 9 +------ .../kogito/jackson/utils/MergeUtilsTest.java | 25 ++----------------- 3 files changed, 4 insertions(+), 32 deletions(-) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java index 54c73966691..e67a2f85b37 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java @@ -51,6 +51,6 @@ private static JsonNode readObject(String json) { @Override public void execute(KogitoProcessContext context) throws Exception { - context.setVariable(SWFConstants.DEFAULT_WORKFLOW_VAR, MergeUtils.merge(node, getWorkflowData(context), false, true)); + context.setVariable(SWFConstants.DEFAULT_WORKFLOW_VAR, MergeUtils.merge(node, getWorkflowData(context))); } } diff --git a/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/MergeUtils.java b/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/MergeUtils.java index 6da6b9c9539..1ddcf12c8c0 100644 --- a/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/MergeUtils.java +++ b/kogito-workitems/kogito-jackson-utils/src/main/java/org/kie/kogito/jackson/utils/MergeUtils.java @@ -39,20 +39,13 @@ public static JsonNode merge(JsonNode src, JsonNode target) { } public static JsonNode merge(JsonNode src, JsonNode target, boolean mergeArray) { - return merge(src, target, mergeArray, false); - } - - public static JsonNode merge(JsonNode src, JsonNode target, boolean mergeArray, boolean cloneSource) { if (target == null || target.isNull() || target.isObject() && target.isEmpty() && src != null && !src.isNull()) { - return cloneSource ? src.deepCopy() : src; + return src; } else if (target.isArray()) { return mergeArray(src, (ArrayNode) target, mergeArray); } else if (target.isObject()) { return mergeObject(src, (ObjectNode) target, mergeArray); } else { - if (cloneSource) { - src = src.deepCopy(); - } if (src.isArray()) { ArrayNode srcArray = (ArrayNode) src; insert(srcArray, target, getExistingNodes(srcArray)); diff --git a/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/MergeUtilsTest.java b/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/MergeUtilsTest.java index 64df78d3e76..c7f034393ed 100644 --- a/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/MergeUtilsTest.java +++ b/kogito-workitems/kogito-jackson-utils/src/test/java/org/kie/kogito/jackson/utils/MergeUtilsTest.java @@ -47,18 +47,7 @@ void testTargetArraySrcInt() { @Test void testTargetIntSrcArray() { - ArrayNode src = ObjectMapperFactory.get().createArrayNode().add(1).add(2).add(3); - JsonNode result = MergeUtils.merge(src, new IntNode(4), false); - assertThat(src).isSameAs(result); - assertThat(JsonObjectUtils.toJavaValue(result)).isEqualTo(Arrays.asList(4, 1, 2, 3)); - } - - @Test - void testTargetIntSrcArrayClone() { - ArrayNode src = ObjectMapperFactory.get().createArrayNode().add(1).add(2).add(3); - JsonNode result = MergeUtils.merge(src, new IntNode(4), false, true); - assertThat(src).isNotSameAs(result); - assertThat(JsonObjectUtils.toJavaValue(result)).isEqualTo(Arrays.asList(4, 1, 2, 3)); + assertThat(JsonObjectUtils.toJavaValue(MergeUtils.merge(ObjectMapperFactory.get().createArrayNode().add(1).add(2).add(3), new IntNode(4), false))).isEqualTo(Arrays.asList(4, 1, 2, 3)); } @Test @@ -120,17 +109,7 @@ void testObjectArrayMergeNoDuplicate() { @Test void testNullMerge() { JsonNode srcNode = ObjectMapperFactory.get().createObjectNode().put("name", "javierito"); - JsonNode result = MergeUtils.merge(srcNode, null); - assertThat(result).isSameAs(srcNode); - - } - - @Test - void testNullMergeClone() { - JsonNode srcNode = ObjectMapperFactory.get().createObjectNode().put("name", "javierito"); - JsonNode result = MergeUtils.merge(srcNode, null, false, true); - assertThat(result).isNotSameAs(srcNode); - assertThat(result).isEqualTo(srcNode); + assertThat(MergeUtils.merge(srcNode, null)).isEqualTo(srcNode); } private static class Person { From 725d0d3009c9b4bc6c56261d24b638c315145fa5 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Tue, 8 Oct 2024 16:33:41 +0200 Subject: [PATCH 3/3] [Fix #3709] Clone node when injecting --- .../kie/kogito/serverless/workflow/actions/InjectAction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java index e67a2f85b37..de05503dcba 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/InjectAction.java @@ -51,6 +51,6 @@ private static JsonNode readObject(String json) { @Override public void execute(KogitoProcessContext context) throws Exception { - context.setVariable(SWFConstants.DEFAULT_WORKFLOW_VAR, MergeUtils.merge(node, getWorkflowData(context))); + context.setVariable(SWFConstants.DEFAULT_WORKFLOW_VAR, MergeUtils.merge(node.deepCopy(), getWorkflowData(context))); } }