Skip to content

Commit

Permalink
[Fix_#3629] Set merge result to array or non object (#3633)
Browse files Browse the repository at this point in the history
* [Fix_#3629] Set merge result to array or non object

* [Fix #3629] Fixing broken tests
  • Loading branch information
fjtirado authored Aug 28, 2024
1 parent 8b50479 commit 8388b0e
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public Collection<GeneratedFile> generate() throws IOException {
if (merged == null) {
merged = read;
} else {
JsonUtils.merge(read, merged);
merged = (ObjectNode) JsonUtils.merge(read, merged);
}
}
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ private JsonUtils() {
* @param src JsonNode to be merged
* @param target JsonNode to merge to
*/
public static void merge(JsonNode src, JsonNode target) {
MergeUtils.merge(src, target, true);
public static JsonNode merge(JsonNode src, JsonNode target) {
return MergeUtils.merge(src, target, true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public void testFullMerge() {
JsonNode node5 = createJson(mapper, mapper.createObjectNode().put("string", "javier"));

JsonNode result = mapper.createObjectNode();
JsonUtils.merge(node1, result);
JsonUtils.merge(node2, result);
JsonUtils.merge(node3, result);
JsonUtils.merge(node4, result);
JsonUtils.merge(node5, result);
result = JsonUtils.merge(node1, result);
result = JsonUtils.merge(node2, result);
result = JsonUtils.merge(node3, result);
result = JsonUtils.merge(node4, result);
result = JsonUtils.merge(node5, result);

assertThat(result.size()).isOne();
JsonNode merged = result.get("merged");
Expand All @@ -73,7 +73,7 @@ public void testArrayIntoObjectMerge() {
JsonNode src = createJson(mapper, createJson(mapper, "property", Arrays.asList(1, 2, 3)));
JsonNode target = createJson(mapper, mapper.createObjectNode().put("property", 4));

JsonUtils.merge(src, target);
target = JsonUtils.merge(src, target);

assertThat(target.size()).isOne();
JsonNode merged = target.get("merged");
Expand All @@ -95,7 +95,7 @@ public void testArrayIntoNewPropertyMerge() {
JsonNode src = createJson(mapper, createJson(mapper, "property", Arrays.asList(1, 2, 3)));
JsonNode target = createJson(mapper, mapper.createObjectNode());

JsonUtils.merge(src, target);
target = JsonUtils.merge(src, target);

assertThat(target.size()).isOne();
JsonNode merged = target.get("merged");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void afterProcessCompleted(ProcessCompletedEvent event) {
}
}).build()) {
Workflow workflow = workflow("HelloWorld").start(inject(new TextNode(GREETING_STRING))).end().build();
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata()).contains(new TextNode(GREETING_STRING));
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata().asText()).isEqualTo(GREETING_STRING);
assertThat(completed.get()).isTrue();
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ void testConstantConcatenation() {
try (StaticWorkflowApplication application = StaticWorkflowApplication.create()) {
Workflow workflow = workflow("PlayingWithExpression").constant("name", "Javierito").function(expr(INTERPOLATION, "\"My name is \"+$CONST.name"))
.start(operation().action(call(INTERPOLATION))).end().build();
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata().get("response").asText()).isEqualTo("My name is Javierito");
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata().asText()).isEqualTo("My name is Javierito");
}
}

Expand Down Expand Up @@ -280,7 +280,7 @@ void testConstantInterpolation() {
try (StaticWorkflowApplication application = StaticWorkflowApplication.create()) {
Workflow workflow = workflow("PlayingWithExpression").constant("name", "Javierito").function(expr(INTERPOLATION, "\"My name is \\($CONST.name)\""))
.start(operation().action(call(INTERPOLATION))).end().build();
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata().get("response").asText()).isEqualTo("My name is Javierito");
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata().asText()).isEqualTo("My name is Javierito");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class StaticWorkflowApplicationTest {
void helloWorld() {
Workflow workflow = helloWorldDef();
try (StaticWorkflowApplication application = StaticWorkflowApplication.create()) {
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata()).contains(new TextNode(GREETING_STRING));
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata().asText()).isEqualTo(GREETING_STRING);
}
}

Expand All @@ -74,7 +74,7 @@ void helloWorldFile() throws IOException {
CharArrayReader reader = new CharArrayReader(writer.toCharArray());
workflow = getWorkflow(reader, WorkflowFormat.JSON);
try (StaticWorkflowApplication application = StaticWorkflowApplication.create()) {
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata()).contains(new TextNode(GREETING_STRING));
assertThat(application.execute(workflow, Collections.emptyMap()).getWorkflowdata().asText()).isEqualTo(GREETING_STRING);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public MergeAction(String inputName, String outputName) {

@Override
public void execute(KogitoProcessContext context) throws Exception {
MergeUtils.merge(ActionUtils.getJsonNode(context, inputName), ActionUtils.getJsonNode(context, outputName));
context.setVariable(outputName, MergeUtils.merge(ActionUtils.getJsonNode(context, inputName), ActionUtils.getJsonNode(context, outputName)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static JsonNode merge(JsonNode src, JsonNode target) {
}

public static JsonNode merge(JsonNode src, JsonNode target, boolean mergeArray) {
if (target == null || target.isNull()) {
if (target == null || target.isNull() || target.isObject() && target.isEmpty() && src != null && !src.isNull()) {
return src;
} else if (target.isArray()) {
return mergeArray(src, (ArrayNode) target, mergeArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ void testTargetIntSrcArray() {

@Test
void testTargetObjectSrcInt() {
assertThat(JsonObjectUtils.toJavaValue(MergeUtils.merge(new IntNode(3), ObjectMapperFactory.get().createObjectNode(), false))).isEqualTo(Collections.singletonMap("response", 3));
assertThat(JsonObjectUtils.toJavaValue(MergeUtils.merge(new IntNode(3), ObjectMapperFactory.get().createObjectNode(), false))).isEqualTo(3);
}

@Test
void testTargetObjectSrcArray() {
assertThat(JsonObjectUtils.toJavaValue(MergeUtils.merge(ObjectMapperFactory.get().createArrayNode().add(1).add(2).add(3), ObjectMapperFactory.get().createObjectNode(), false)))
.isEqualTo(Collections.singletonMap("response", Arrays.asList(1, 2, 3)));
.isEqualTo(Arrays.asList(1, 2, 3));
}

@Test
Expand Down

0 comments on commit 8388b0e

Please sign in to comment.