From c3ba9723e7d76b459431ececc429f8a0cc8e5da4 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 3 Jun 2024 10:45:12 -0400 Subject: [PATCH] ResultPath=$ replaces complete output ok, https://states-language.net/#error-output > The default value, if the "ResultPath" field is not provided, is "$", meaning that the output consists entirely of the Error Output. Which differs from the previous code https://github.com/ManageIQ/floe/blob/master/lib/floe/workflow/reference_path.rb#L28-L29 > If the payload is '$' then merge the value into the context This fixes issues where the output is a simple class inserting at the top level --- lib/floe/workflow/reference_path.rb | 7 ++----- spec/workflow/reference_path_spec.rb | 12 ++++++++++-- spec/workflow/states/task_spec.rb | 4 ++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/floe/workflow/reference_path.rb b/lib/floe/workflow/reference_path.rb index 2633c0fa..333624c5 100644 --- a/lib/floe/workflow/reference_path.rb +++ b/lib/floe/workflow/reference_path.rb @@ -25,12 +25,9 @@ def get(context) def set(context, value) result = context.dup - # If the payload is '$' then merge the value into the context - # otherwise store the value under the path - # - # TODO: how to handle non-hash values, raise error if path=$ and value not a hash? + # If the payload is '$' then replace the output with the value if path.empty? - result.merge!(value) + result = value.dup else child = result keys = path.dup diff --git a/spec/workflow/reference_path_spec.rb b/spec/workflow/reference_path_spec.rb index d4aab8ac..4c2c70e9 100644 --- a/spec/workflow/reference_path_spec.rb +++ b/spec/workflow/reference_path_spec.rb @@ -50,7 +50,7 @@ describe "#set" do let(:payload) { "$" } - let(:input) { {} } + let(:input) { {"old" => "key"} } context "with a simple path" do it "sets the output at the top-level" do @@ -58,11 +58,19 @@ end end + context "with a top level path" do + let(:payload) { "$.hash" } + + it "sets the output at the correct nested level" do + expect(subject.set(input, "foo" => "bar")).to eq("old" => "key", "hash" => {"foo" => "bar"}) + end + end + context "with a nested path" do let(:payload) { "$.nested.hash" } it "sets the output at the correct nested level" do - expect(subject.set(input, "foo" => "bar")).to eq("nested" => {"hash" => {"foo" => "bar"}}) + expect(subject.set(input, "foo" => "bar")).to eq("old" => "key", "nested" => {"hash" => {"foo" => "bar"}}) end end diff --git a/spec/workflow/states/task_spec.rb b/spec/workflow/states/task_spec.rb index 4450e6ce..904da0fb 100644 --- a/spec/workflow/states/task_spec.rb +++ b/spec/workflow/states/task_spec.rb @@ -51,7 +51,7 @@ workflow.current_state.run_nonblock! - expect(ctx.output).to eq("foo" => {"bar" => "baz"}, "bar" => {"baz" => "foo"}, "response" => ["192.168.1.2"]) + expect(ctx.output).to eq("response" => ["192.168.1.2"]) end context "with an error" do @@ -80,7 +80,7 @@ workflow.current_state.run_nonblock! - expect(ctx.output).to eq("foo" => {"bar" => "baz"}, "bar" => {"baz" => "foo"}, "ip_addrs" => ["192.168.1.2"]) + expect(ctx.output).to eq("ip_addrs" => ["192.168.1.2"]) end end