-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
Changes from all commits
4433bce
8508e54
4be6bdd
176a4b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}"); | ||
} | ||
} | ||
|
@@ -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 }); | ||
}; | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed unnecessary semicolon after the statement. This appears to be a formatting cleanup that's unrelated to the main fix. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
propertyInfo.SetValue(data, resolvedValue, new object[] { items[1] }); | ||
}; | ||
|
||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
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) | ||
|
@@ -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; | ||
} | ||
|
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; } = []; | ||
} |
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(); | ||
} | ||
} |
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; } | ||
} |
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 | ||||||
} | ||||||
} | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra trailing whitespace after the closing brace. Consider removing the trailing spaces for consistency.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
} | ||||||
] | ||||||
} |
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 | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed unnecessary semicolon after the statement. This appears to be a formatting cleanup that's unrelated to the main fix.
Copilot uses AI. Check for mistakes.