Skip to content

fix: #1344 convert destObj to the correct property type before settin… #1346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 86 additions & 4 deletions src/WorkflowCore.DSL/Services/DefinitionLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,21 @@ private void AttachInputs(StepSourceV1 source, Type dataType, Type stepType, Wor
continue;
}

if ((input.Value is IDictionary<string, object>) || (input.Value is IDictionary<object, object>))
if (input.Value is IDictionary<string, object> || input.Value is IDictionary<object, object>)
{
var acn = BuildObjectInputAction(input, dataParameter, contextParameter, environmentVarsParameter, stepProperty);
step.Inputs.Add(new ActionParameter<IStepBody, object>(acn));
continue;
}

if (input.Value is IEnumerable<object> list)
{
var acn = BuildListInputAction(list, dataParameter, contextParameter, environmentVarsParameter,
stepProperty);
step.Inputs.Add(new ActionParameter<IStepBody, object>(acn));
continue;
}

throw new ArgumentException($"Unknown type for input {input.Key} on {source.Id}");
}
}
Expand Down Expand Up @@ -253,7 +261,7 @@ private void AttachDirectlyOutput(KeyValuePair<string, string> output, WorkflowS

Action<IStepBody, object> acn = (pStep, pData) =>
{
object resolvedValue = sourceExpr.Compile().DynamicInvoke(pStep); ;
object resolvedValue = sourceExpr.Compile().DynamicInvoke(pStep);
propertyInfo.SetValue(pData, resolvedValue, new object[] { output.Key });
};

Expand Down Expand Up @@ -306,7 +314,7 @@ private void AttachNestedOutput(KeyValuePair<string, string> output, WorkflowSte
{
var targetExpr = Expression.Lambda(memberExpression, dataParameter);
object data = targetExpr.Compile().DynamicInvoke(pData);
object resolvedValue = sourceExpr.Compile().DynamicInvoke(pStep); ;
object resolvedValue = sourceExpr.Compile().DynamicInvoke(pStep);
propertyInfo.SetValue(data, resolvedValue, new object[] { items[1] });
};

Expand Down Expand Up @@ -379,6 +387,80 @@ void acn(IStepBody pStep, object pData, IStepExecutionContext pContext)
return acn;
}

private static Action<IStepBody, object, IStepExecutionContext> BuildListInputAction(
IEnumerable<object> input,
ParameterExpression dataParameter,
ParameterExpression contextParameter,
ParameterExpression environmentVarsParameter,
PropertyInfo stepProperty)
{
void acn(IStepBody pStep, object pData, IStepExecutionContext pContext)
{
if (input == null)
throw new ArgumentNullException(nameof(input));

var itemType = stepProperty.PropertyType.IsArray
? stepProperty.PropertyType.GetElementType()
: stepProperty.PropertyType.GetGenericArguments().FirstOrDefault();

if (itemType == null)
throw new InvalidOperationException("Unable to determine the item type for stepProperty.");

var processedItems = new List<object>();

foreach (var item in input)
{
var obj = JObject.FromObject(item);
var stack = new Stack<JObject>();
stack.Push(obj);

while (stack.Count > 0)
{
var current = stack.Pop();
foreach (var prop in current.Properties().ToList())
{
if (prop.Name.StartsWith("@"))
{
var expr = DynamicExpressionParser.ParseLambda(
new[] { dataParameter, contextParameter, environmentVarsParameter },
typeof(object),
prop.Value.ToString());

var resolved = expr.Compile().DynamicInvoke(pData, pContext,
Environment.GetEnvironmentVariables());
current.Remove(prop.Name);
current.Add(prop.Name.TrimStart('@'), JToken.FromObject(resolved));
}
}

foreach (var child in current.Children<JObject>())
stack.Push(child);
}

processedItems.Add(obj.ToObject(itemType));
}

if (stepProperty.PropertyType.IsArray)
{
var array = Array.CreateInstance(itemType, processedItems.Count);
for (var i = 0; i < processedItems.Count; i++)
array.SetValue(processedItems[i], i);
stepProperty.SetValue(pStep, array);
}
else
{
var listInstance = Activator.CreateInstance(typeof(List<>).MakeGenericType(itemType));
var addMethod = listInstance.GetType().GetMethod("Add");
foreach (var item in processedItems)
addMethod?.Invoke(listInstance, new[] { item });

stepProperty.SetValue(pStep, listInstance);
}
}

return acn;
}

private static Action<IStepBody, object, IStepExecutionContext> BuildObjectInputAction(KeyValuePair<string, object> input, ParameterExpression dataParameter, ParameterExpression contextParameter, ParameterExpression environmentVarsParameter, PropertyInfo stepProperty)
{
void acn(IStepBody pStep, object pData, IStepExecutionContext pContext)
Expand All @@ -405,7 +487,7 @@ void acn(IStepBody pStep, object pData, IStepExecutionContext pContext)
stack.Push(child);
}

stepProperty.SetValue(pStep, destObj);
stepProperty.SetValue(pStep, destObj.ToObject(stepProperty.PropertyType));
}
return acn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,53 @@ public void should_execute_json_workflow_with_dynamic_data()
data["Counter6"].Should().Be(1);
data["Counter10"].Should().Be(1);
}


[Fact]
public void should_execute_json_workflow_with_complex_input_type()
{
var initialData = new FlowData();
var workflowId = StartWorkflow(TestAssets.Utils.GetTestDefinitionJsonComplexInputProperty(), initialData);
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));

var data = GetData<FlowData>(workflowId);
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
UnhandledStepErrors.Count.Should().Be(0);
data.Assignee.Should().NotBeNull();
data.Assignee.Name.Should().Be("John Doe");
data.Assignee.UnitInfo.Should().NotBeNull();
data.Assignee.UnitInfo.Name.Should().Be("IT Department");

}

[Fact]
public void should_execute_json_workflow_with_list_of_complex_input_type()
{
var initialData = new FlowData();
var workflowId = StartWorkflow(TestAssets.Utils.GetTestDefinitionJsonListOfComplexInputProperty(), initialData);
WaitForWorkflowToComplete(workflowId, TimeSpan.FromSeconds(30));

var data = GetData<FlowData>(workflowId);
GetStatus(workflowId).Should().Be(WorkflowStatus.Complete);
UnhandledStepErrors.Count.Should().Be(0);

data.AssigneeList.Should().NotBeNullOrEmpty();
data.AssigneeList.Count.Should().Be(2);

data.Assignee.Should().NotBeNull();
data.Assignee.Name.Should().Be("John Doe");
data.Assignee.UnitInfo?.Name.Should().Be("IT Department");

data.AssigneeList[0]?.Name.Should().Be(@"Nurlan Mikayilov");
data.AssigneeList[0]?.UnitInfo?.Name.Should().Be("IT Department");

data.AssigneeList[1]?.Name.Should().Be(@"Jala Mammadova");
data.AssigneeList[1]?.UnitInfo?.Name.Should().Be("General Department");

data.AssigneeArray[0]?.Name.Should().Be(@"Amin Nabiyev");
data.AssigneeArray[0]?.UnitInfo?.Name.Should().Be("IT Department");


}
}
}
13 changes: 13 additions & 0 deletions test/WorkflowCore.TestAssets/DataTypes/FlowData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using WorkflowCore.TestAssets.Steps;

namespace WorkflowCore.TestAssets.DataTypes;

public class FlowData
{
public AssigneeInfo Assignee { get; set; } = new();
public List<AssigneeInfo> AssigneeList { get; set; } = [];
public AssigneeInfo[] AssigneeArray { get; set; } = [];
}
37 changes: 37 additions & 0 deletions test/WorkflowCore.TestAssets/Steps/AssignTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using WorkflowCore.TestAssets.DataTypes;

namespace WorkflowCore.TestAssets.Steps;

public class AssignTask : StepBody
{
public AssigneeInfo Assignee { get; set; }
public List<AssigneeInfo> AssigneeList { get; set; } = [];

public AssigneeInfo[] AssigneeArray { get; set; } = [];

public override ExecutionResult Run(IStepExecutionContext context)
{
if (context.Workflow.Data is FlowData flowData)
{
if (Assignee != null)
{
flowData.Assignee = new AssigneeInfo
{
Id = Assignee.Id,
Name = Assignee.Name,
MemberType = Assignee.MemberType,
UnitInfo = Assignee.UnitInfo
};
}

flowData.AssigneeList.AddRange(AssigneeList);
flowData.AssigneeArray = AssigneeArray.ToArray();
}
return ExecutionResult.Next();
}
}
20 changes: 20 additions & 0 deletions test/WorkflowCore.TestAssets/Steps/AssigneeInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Linq;

namespace WorkflowCore.TestAssets.Steps;

public class AssigneeInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int MemberType { get; set; }

public UnitInfo UnitInfo { get; set; }
}

public class UnitInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int UnitType { get; set; }
}
11 changes: 11 additions & 0 deletions test/WorkflowCore.TestAssets/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public static string GetTestDefinitionJsonMissingInputProperty()
{
return File.ReadAllText("stored-def-missing-input-property.json");
}


public static string GetTestDefinitionJsonComplexInputProperty()
{
return File.ReadAllText("def-complex-input-property.json");
}

public static string GetTestDefinitionJsonListOfComplexInputProperty()
{
return File.ReadAllText("def-list-of-complex-input-property.json");
}
}
}

7 changes: 7 additions & 0 deletions test/WorkflowCore.TestAssets/WorkflowCore.TestAssets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
<None Remove="stored-definition.yaml" />
<None Remove="stored-dynamic-definition.json" />
<None Remove="stored-dynamic-definition.yaml" />
<None Remove="def-complex-input-property.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="def-list-of-complex-input-property.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="stored-dynamic-definition.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
Expand All @@ -31,6 +35,9 @@
<EmbeddedResource Include="stored-def-missing-input-property.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="def-complex-input-property.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 23 additions & 0 deletions test/WorkflowCore.TestAssets/def-complex-input-property.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Id": "FlowWithComplexInputType",
"Version": 1,
"DataType": "WorkflowCore.TestAssets.DataTypes.FlowData, WorkflowCore.TestAssets",
"Steps": [
{
"Id": "AssignTask",
"StepType": "WorkflowCore.TestAssets.Steps.AssignTask, WorkflowCore.TestAssets",
"Inputs": {
"Assignee": {
"id": 1,
"name": "John Doe",
"memberType": 1,
"unitInfo": {
"id": 1,
"name": "IT Department",
"unitType": 1
}
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"Id": "FlowWithListOfComplexInputType",
"Version": 1,
"DataType": "WorkflowCore.TestAssets.DataTypes.FlowData, WorkflowCore.TestAssets",
"Steps": [
{
"Id": "AssignTask",
"StepType": "WorkflowCore.TestAssets.Steps.AssignTask, WorkflowCore.TestAssets",
"Inputs": {
"AssigneeList": [
{
"id": 1,
"name": "Nurlan Mikayilov",
"memberType": 1,
"unitInfo": {
"id": 2,
"name": "IT Department",
"unitType": 1
}
},
{
"id": 2,
"name": "Jala Mammadova",
"memberType": 2,
"unitInfo": {
"id": 1,
"name": "General Department",
"unitType": 1
}
}
],
"AssigneeArray": [
{
"id": 3,
"name": "Amin Nabiyev",
"memberType": 1,
"unitInfo": {
"id": 2,
"name": "IT Department",
"unitType": 1
}
}
],
"Assignee": {
"id": 1,
"name": "John Doe",
"memberType": 1,
"unitInfo": {
"id": 1,
"name": "IT Department",
"unitType": 1
}
}
}
}
]
}
Loading