Skip to content

A platform for community discussion. Free, open, simple.

FIX: Merge node producing no output after rewiring its connections (#44987)

Osama Sayeghc28a253205e3
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
index b5dcade43f6..be1193fe883 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
@@ -237,6 +237,10 @@ module DiscourseWorkflows
Array.new(max_index + 1) { |index| @input_groups[index] || [] }
end
+ def input_indexes
+ @input_groups.keys.sort
+ end
+
def input_context
InputContext.from_node_context(get_context(:node))
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/merge/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/merge/v1.rb
index 9ea6d0ab12a..606d5064ae3 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/merge/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/merge/v1.rb
@@ -69,8 +69,9 @@ module DiscourseWorkflows
def execute(exec_ctx)
return [append_inputs(exec_ctx)] unless combine_mode?(exec_ctx)
- @input_item_indexes = item_indexes_by_input(exec_ctx.inputs)
- [combine_by_position(exec_ctx.inputs, combine_config(exec_ctx))]
+ slots = delivered_slots(exec_ctx)
+ @input_item_indexes = item_indexes_by_slot(slots)
+ [combine_by_position(slots, combine_config(exec_ctx))]
end
private
@@ -92,17 +93,18 @@ module DiscourseWorkflows
exec_ctx.inputs.flatten(1)
end
- def combine_by_position(inputs, config)
- preferred_index = preferred_input_index(config, inputs.length)
- count =
- if config["include_unpaired"]
- inputs.map(&:length).max || 0
- else
- inputs.map(&:length).min || 0
- end
+ def delivered_slots(exec_ctx)
+ exec_ctx.input_indexes.map { |slot_index| [slot_index, exec_ctx.input_items(slot_index)] }
+ end
+
+ def combine_by_position(slots, config)
+ preferred_index = preferred_input_index(config, slots.length)
+ lengths = slots.map { |_slot_index, items| items.length }
+ count = config["include_unpaired"] ? lengths.max || 0 : lengths.min || 0
count.times.map do |index|
- entries = inputs.map { |input| input[index] || { "json" => {} } }
+ entries =
+ slots.map { |slot_index, items| [slot_index, items[index] || { "json" => {} }] }
merge_entries(entries, config, preferred_index:)
end
end
@@ -111,12 +113,12 @@ module DiscourseWorkflows
paired_items = paired_items(entries)
entries = suffix_entries(entries) if config["resolve_clash"] == "add_suffix"
ordered_entries = entries.dup
- preferred = ordered_entries.delete_at(preferred_index) || { "json" => {} }
+ preferred = ordered_entries.delete_at(preferred_index) || [nil, { "json" => {} }]
ordered_entries << preferred
- wrap(merge_hashes(ordered_entries.map { |entry| entry.fetch("json") { {} } })).merge(
- "pairedItem" => paired_items,
- )
+ wrap(
+ merge_hashes(ordered_entries.map { |_slot_index, entry| entry.fetch("json") { {} } }),
+ ).merge("pairedItem" => paired_items)
end
def merge_hashes(hashes)
@@ -135,38 +137,32 @@ module DiscourseWorkflows
end
def suffix_entries(entries)
- entries.map.with_index do |entry, index|
+ entries.map do |slot_index, entry|
json =
entry
.fetch("json") { {} }
.each_with_object({}) do |(key, value), result|
- result["#{key}_#{index + 1}"] = value
+ result["#{key}_#{slot_index + 1}"] = value
end
- entry.merge("json" => json)
+ [slot_index, entry.merge("json" => json)]
end
end
def paired_items(entries)
- entries.each_with_index.filter_map do |entry, input_index|
- paired_item_for_entry(entry, input_index)
+ entries.filter_map do |slot_index, entry|
+ @input_item_indexes[[slot_index, entry.object_id]]
end
end
- def paired_item_for_entry(entry, input_index)
- item_index = @input_item_indexes&.dig(input_index, entry.object_id)
- return unless item_index
-
- { "input" => input_index, "item" => item_index }
- end
-
- def item_indexes_by_input(inputs)
- inputs
- .each_with_index
- .each_with_object({}) do |(items, input_index), indexes|
- indexes[input_index] = items.each_with_index.to_h do |item, item_index|
- [item.object_id, item_index]
- end
+ def item_indexes_by_slot(slots)
+ slots.each_with_object({}) do |(slot_index, items), indexes|
+ items.each_with_index do |item, item_index|
+ indexes[[slot_index, item.object_id]] = {
+ "input" => slot_index,
+ "item" => item_index,
+ }
end
+ end
end
def preferred_input_index(config, input_count)
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_merge_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_merge_spec.rb
new file mode 100644
index 00000000000..59eab8d5a16
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_merge_spec.rb
@@ -0,0 +1,204 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Executor do
+ fab!(:user)
+
+ def run_workflow(&block)
+ graph = build_workflow_graph(&block)
+ workflow = Fabricate(:discourse_workflows_workflow, created_by: user, published: true, **graph)
+
+ described_class.new(workflow, "trigger-1", {}).run
+ end
+
+ def context_output(execution, name)
+ execution.execution_data.context_data[name]
+ end
+
+ def if_config(expected_kind)
+ {
+ "combinator" => "and",
+ "conditions" => [
+ {
+ "id" => "1",
+ "leftValue" => "={{ $json.kind }}",
+ "rightValue" => expected_kind,
+ "operator" => {
+ "type" => "string",
+ "operation" => "equals",
+ },
+ },
+ ],
+ }
+ end
+
+ def source_node(graph, json)
+ graph.node "source",
+ "action:set_fields",
+ name: "Source",
+ configuration: {
+ "mode" => "raw",
+ "include_other_fields" => false,
+ "json_output" => json,
+ }
+ end
+
+ def extra_node(graph)
+ graph.node "extra",
+ "action:set_fields",
+ name: "Extra",
+ configuration: {
+ "mode" => "manual",
+ "include_other_fields" => false,
+ "assignments" => {
+ "assignments" => [{ "name" => "extra", "value" => "yes", "type" => "string" }],
+ },
+ }
+ end
+
+ it "combines inputs connected on non-contiguous slots and keeps the slot in pairedItem" do
+ execution =
+ run_workflow do |g|
+ g.node "trigger-1", "trigger:manual"
+ source_node(g, '{"id": 1}')
+ extra_node(g)
+ g.node "merge",
+ "flow:merge",
+ name: "Merge",
+ configuration: {
+ "mode" => "combine",
+ "resolve_clash" => "prefer_last",
+ }
+ g.chain "trigger-1", "source"
+ g.connect "source", "extra"
+ g.connect "source", "merge", input: "input_1"
+ g.connect "extra", "merge", input: "input_3"
+ end
+
+ expect(execution.status).to eq("success")
+ expect(context_output(execution, "Merge")).to eq(
+ [
+ {
+ "json" => {
+ "id" => 1,
+ "extra" => "yes",
+ },
+ "pairedItem" => [{ "input" => 0, "item" => 0 }, { "input" => 2, "item" => 0 }],
+ },
+ ],
+ )
+ end
+
+ it "resolves previous-node .item through a combine with a slot gap" do
+ execution =
+ run_workflow do |g|
+ g.node "trigger-1", "trigger:manual"
+ source_node(g, '{"id": 1}')
+ extra_node(g)
+ g.node "merge",
+ "flow:merge",
+ name: "Merge",
+ configuration: {
+ "mode" => "combine",
+ "resolve_clash" => "prefer_last",
+ }
+ g.node "set",
+ "action:set_fields",
+ name: "Set",
+ configuration: {
+ "mode" => "manual",
+ "include_other_fields" => false,
+ "assignments" => {
+ "assignments" => [
+ {
+ "name" => "linked_id",
+ "value" => "={{ $('Source').item.json.id }}",
+ "type" => "number",
+ },
+ ],
+ },
+ }
+ g.chain "trigger-1", "source"
+ g.connect "source", "extra"
+ g.connect "source", "merge", input: "input_1"
+ g.connect "extra", "merge", input: "input_3"
+ g.chain "merge", "set"
+ end
+
+ expect(execution.status).to eq("success")
+ expect(context_output(execution, "Set").map { |item| item.dig("json", "linked_id") }).to eq([1])
+ end
+
+ it "keeps per-slot lineage when one output feeds two slots" do
+ execution =
+ run_workflow do |g|
+ g.node "trigger-1", "trigger:manual"
+ source_node(g, '{"id": 1}')
+ g.node "merge", "flow:merge", name: "Merge", configuration: { "mode" => "combine" }
+ g.chain "trigger-1", "source"
+ g.connect "source", "merge", input: "input_1"
+ g.connect "source", "merge", input: "input_2"
+ end
+
+ expect(execution.status).to eq("success")
+ expect(context_output(execution, "Merge")).to eq(
+ [
+ {
+ "json" => {
+ "id_1" => 1,
+ "id_2" => 1,
+ },
+ "pairedItem" => [{ "input" => 0, "item" => 0 }, { "input" => 1, "item" => 0 }],
+ },
+ ],
+ )
+ end
+
+ it "combines only the delivered inputs when an upstream branch stopped" do
+ execution =
+ run_workflow do |g|
+ g.node "trigger-1", "trigger:manual"
+ source_node(g, '{"id": 1}')
+ g.node "filter", "condition:filter", name: "Filter", configuration: if_config("never")
+ extra_node(g)
+ g.node "merge",
+ "flow:merge",
+ name: "Merge",
+ configuration: {
+ "mode" => "combine",
+ "resolve_clash" => "prefer_last",
+ }
+ g.chain "trigger-1", "source"
+ g.connect "source", "merge", input: "input_1"
+ g.chain "source", "filter", "extra"
+ g.connect "extra", "merge", input: "input_2"
+ end
+
+ expect(execution.status).to eq("success")
+ expect(context_output(execution, "Merge").map { |item| item["json"] }).to eq([{ "id" => 1 }])
+ end
+
+ it "produces no items when a connected input delivered an empty list" do
+ execution =
+ run_workflow do |g|
+ g.node "trigger-1", "trigger:manual"
+ source_node(g, '{"kind": "a"}')
+ g.node "if-a", "condition:if", name: "If A", configuration: if_config("a")
+ g.node "if-b", "condition:if", name: "If B", configuration: if_config("b")
+ g.node "merge",
+ "flow:merge",
+ name: "Merge",
+ configuration: {
+ "mode" => "combine",
+ "resolve_clash" => "prefer_last",
+ }
+ g.chain "trigger-1", "source"
+ g.connect "source", "if-a"
+ g.connect "source", "if-b"
+ g.connect "if-a", "merge", output: "true", input: "input_1"
+ g.connect "if-b", "merge", output: "true", input: "input_2"
+ end
+
+ expect(execution.status).to eq("success")
+ expect(context_output(execution, "Merge")).to eq([])
+ end
+end