Skip to content
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

Don't Serialize in GetValue<> of Dictionary Extensions #16224

Merged
merged 5 commits into from
Jun 9, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,19 @@ public override async Task OnWorkflowRestartingAsync(WorkflowExecutionContext wo

if (workflowContext.Input.TryGetValue(ContentEventConstants.ContentEventInputKey, out var contentEvent))
{
var contentEventContext = ((JsonObject)contentEvent).ToObject<ContentEventContext>();
if (contentEvent is not JsonObject jsonObject)
{
jsonObject = [];
if (contentEvent is Dictionary<string, object> items)
{
foreach (var item in items)
{
jsonObject[item.Key] = JsonSerializer.SerializeToNode(item.Value);
}
}
}

var contentEventContext = jsonObject.ToObject<ContentEventContext>();

if (contentEventContext?.ContentItemVersionId != null)
{
Expand All @@ -93,11 +105,23 @@ public override async Task OnWorkflowRestartingAsync(WorkflowExecutionContext wo

if (contentItem == null && workflowContext.Input.TryGetValue(ContentEventConstants.ContentItemInputKey, out var contentItemEvent))
{
var item = ((JsonObject)contentItemEvent).ToObject<ContentItem>();
if (contentItemEvent is not JsonObject jsonObject)
{
jsonObject = [];
if (contentEvent is Dictionary<string, object> items)
{
foreach (var item in items)
{
jsonObject[item.Key] = JsonSerializer.SerializeToNode(item.Value);
}
}
}

var existingContentItem = jsonObject.ToObject<ContentItem>();

if (item?.ContentItemId != null)
if (existingContentItem?.ContentItemId != null)
{
contentItem = await ContentManager.GetAsync(item.ContentItemId);
contentItem = await ContentManager.GetAsync(existingContentItem.ContentItemId);
}
}

Expand Down Expand Up @@ -135,8 +159,8 @@ protected virtual async Task<IContent> GetContentAsync(WorkflowExecutionContext
else
{
// If no expression was provided, see if the content item was provided as an input or as a property.
content = workflowContext.Input.GetValue<ContentItem>(ContentEventConstants.ContentItemInputKey)
?? workflowContext.Properties.GetValue<ContentItem>(ContentEventConstants.ContentItemInputKey);
content = workflowContext.Input.GetValue<IContent>(ContentEventConstants.ContentItemInputKey)
?? workflowContext.Properties.GetValue<IContent>(ContentEventConstants.ContentItemInputKey);
}

if (content?.ContentItem?.ContentItemId != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public async override Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecuti
}
else
{
contentItem = workflowContext.Input.GetValue<ContentItem>(ContentEventConstants.ContentItemInputKey)?.ContentItem;
contentItem = workflowContext.Input.GetValue<IContent>(ContentEventConstants.ContentItemInputKey)?.ContentItem;
}

if (contentItem == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private Task<IEnumerable<WorkflowExecutionContext>> TriggerWorkflowEventAsync(st
var input = new Dictionary<string, object>
{
{ ContentEventConstants.ContentItemInputKey, contentItem },
{ ContentEventConstants.ContentEventInputKey, contentEvent }
{ ContentEventConstants.ContentEventInputKey, contentEvent },
};

return _workflowManager.TriggerEventAsync(name, input, correlationId: contentItem.ContentItemId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public override async Task<IDisplayResult> UpdateAsync(ContentItem model, IUpdat
{
{ ContentEventConstants.UserActionInputKey, action },
{ ContentEventConstants.ContentItemInputKey, model },
{ ContentEventConstants.ContentEventInputKey, contentEvent }
{ ContentEventConstants.ContentEventInputKey, contentEvent },
};

await _workflowManager.TriggerEventAsync(nameof(UserTaskEvent), input, correlationId: model.ContentItemId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Text.Json;

namespace OrchardCore.Workflows.Helpers
{
Expand All @@ -25,14 +24,7 @@ public static TValue GetValue<TValue>(this IDictionary<string, object> dictionar
return default;
}

if (value is not TValue castedValue)
{
var json = JConvert.SerializeObject(value);

return JConvert.DeserializeObject<TValue>(json);
}

return castedValue;
return (TValue)value;
}

/// <summary>
Expand Down
Loading