Skip to content

Commit a85c335

Browse files
committed
sub workflow propagation as output to parent
1 parent 6c6eb43 commit a85c335

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/WorkflowCore/Services/FluentBuilders/StepBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,16 @@ public IStepBuilder<TData, SubWorkflowStepBody> SubWorkflow(
603603

604604
if (parameters != null)
605605
stepBuilder.Input((step) => step.Parameters, parameters);
606+
607+
// use the result of the sub workflow as an output
608+
// merge it with parent workflow data
609+
stepBuilder.Output((body, data) =>
610+
{
611+
foreach (var prop in typeof(TData).GetProperties())
612+
{
613+
prop.SetValue(data, prop.GetValue(body.Result));
614+
}
615+
});
606616

607617
Step.Outcomes.Add(new ValueOutcome { NextStep = newStep.Id });
608618
return stepBuilder;

test/WorkflowCore.IntegrationTests/Scenarios/ApprovalScenario.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using FluentAssertions;
3+
using Newtonsoft.Json.Linq;
34
using WorkflowCore.Interface;
45
using WorkflowCore.Models;
56
using WorkflowCore.Testing;
@@ -58,7 +59,12 @@ public void Build(IWorkflowBuilder<ApprovalInput> builder)
5859
.Do(then
5960
=> then
6061
.WaitFor("Approved", e => e.Id)
61-
.Output(i => i.Approved, step => step.EventData)
62+
.Output((w, input) =>
63+
{
64+
var j = JObject.FromObject(w.EventData);
65+
input.Approved = j["Approved"].Value<bool>();
66+
input.Message= j["Message"].Value<string>();
67+
})
6268
.EndWorkflow()
6369
)
6470
.Join();
@@ -87,15 +93,25 @@ public void Scenario(bool approved)
8793
WaitForEventSubscription("Approved", workflowId, TimeSpan.FromSeconds(5));
8894
UnhandledStepErrors.Should().BeEmpty();
8995

90-
Host.PublishEvent("Approved", workflowId, approved);
96+
Host.PublishEvent("Approved", workflowId, new
97+
{
98+
Approved = approved,
99+
Message = "message " + approved
100+
});
91101

92102
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(10));
93103

94104
System.Threading.Thread.Sleep(2000);
95105

96106
UnhandledStepErrors.Should().BeEmpty();
97107
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
98-
GetData(workflowId).Approved.Should().Be(approved);
108+
GetData(workflowId).ShouldBeEquivalentTo(new ApprovalInput
109+
{
110+
Id = eventKey,
111+
Approved = approved,
112+
Message = "message " + approved,
113+
TimeSpan = TimeSpan.FromMinutes(10)
114+
});
99115
}
100116

101117
[Fact]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using WorkflowCore.IntegrationTests.Scenarios;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
namespace WorkflowCore.Tests.Sqlite.Scenarios
8+
{
9+
[Collection("Sqlite collection")]
10+
public class SqliteApprovalScenario : ApprovalScenario
11+
{
12+
protected override void ConfigureServices(IServiceCollection services)
13+
{
14+
services.AddWorkflow(cfg =>
15+
{
16+
cfg.UseSqlite($"Data Source=wfc-tests-{DateTime.Now.Ticks}.db;", true);
17+
cfg.UsePollInterval(TimeSpan.FromSeconds(2));
18+
});
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)