From 5cc7130b6aade39daa523d147602180b6e802a90 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 3 Jun 2024 11:01:15 -0700 Subject: [PATCH 1/2] Don't Serialize in GetValue<> of Dictionary Extensions --- .../Workflows/Activities/ContentActivity.cs | 36 +++++++++++++++---- .../Workflows/Activities/UpdateContentTask.cs | 2 +- .../Workflows/Handlers/ContentsHandler.cs | 2 +- .../Drivers/UserTaskEventContentDriver.cs | 2 +- .../Helpers/DictionaryExtensions.cs | 10 +----- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/ContentActivity.cs b/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/ContentActivity.cs index d733a8426bf..efbdff7d968 100644 --- a/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/ContentActivity.cs +++ b/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/ContentActivity.cs @@ -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(); + if (contentEvent is not JsonObject er) + { + er = []; + if (contentEvent is Dictionary items) + { + foreach (var item in items) + { + er[item.Key] = JsonSerializer.SerializeToNode(item.Value); + } + } + } + + var contentEventContext = er.ToObject(); if (contentEventContext?.ContentItemVersionId != null) { @@ -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(); + if (contentItemEvent is not JsonObject er) + { + er = []; + if (contentEvent is Dictionary items) + { + foreach (var item in items) + { + er[item.Key] = JsonSerializer.SerializeToNode(item.Value); + } + } + } + + var existingContentItem = er.ToObject(); - if (item?.ContentItemId != null) + if (existingContentItem?.ContentItemId != null) { - contentItem = await ContentManager.GetAsync(item.ContentItemId); + contentItem = await ContentManager.GetAsync(existingContentItem.ContentItemId); } } @@ -135,8 +159,8 @@ protected virtual async Task 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(ContentEventConstants.ContentItemInputKey) - ?? workflowContext.Properties.GetValue(ContentEventConstants.ContentItemInputKey); + content = workflowContext.Input.GetValue(ContentEventConstants.ContentItemInputKey) + ?? workflowContext.Properties.GetValue(ContentEventConstants.ContentItemInputKey); } if (content?.ContentItem?.ContentItemId != null) diff --git a/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/UpdateContentTask.cs b/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/UpdateContentTask.cs index bbe8a56ed67..8190b52a9d8 100644 --- a/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/UpdateContentTask.cs +++ b/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/UpdateContentTask.cs @@ -98,7 +98,7 @@ public async override Task ExecuteAsync(WorkflowExecuti } else { - contentItem = workflowContext.Input.GetValue(ContentEventConstants.ContentItemInputKey)?.ContentItem; + contentItem = workflowContext.Input.GetValue(ContentEventConstants.ContentItemInputKey)?.ContentItem; } if (contentItem == null) diff --git a/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Handlers/ContentsHandler.cs b/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Handlers/ContentsHandler.cs index 8b7f8d7e3fb..51f1cff6830 100644 --- a/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Handlers/ContentsHandler.cs +++ b/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Handlers/ContentsHandler.cs @@ -66,7 +66,7 @@ private Task> TriggerWorkflowEventAsync(st var input = new Dictionary { { ContentEventConstants.ContentItemInputKey, contentItem }, - { ContentEventConstants.ContentEventInputKey, contentEvent } + { ContentEventConstants.ContentEventInputKey, contentEvent }, }; return _workflowManager.TriggerEventAsync(name, input, correlationId: contentItem.ContentItemId); diff --git a/src/OrchardCore.Modules/OrchardCore.Workflows/UserTasks/Drivers/UserTaskEventContentDriver.cs b/src/OrchardCore.Modules/OrchardCore.Workflows/UserTasks/Drivers/UserTaskEventContentDriver.cs index 7b91a897e59..23515c7193e 100644 --- a/src/OrchardCore.Modules/OrchardCore.Workflows/UserTasks/Drivers/UserTaskEventContentDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Workflows/UserTasks/Drivers/UserTaskEventContentDriver.cs @@ -93,7 +93,7 @@ public override async Task 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); diff --git a/src/OrchardCore/OrchardCore.Workflows.Abstractions/Helpers/DictionaryExtensions.cs b/src/OrchardCore/OrchardCore.Workflows.Abstractions/Helpers/DictionaryExtensions.cs index 655972a33e3..fda94622fd8 100644 --- a/src/OrchardCore/OrchardCore.Workflows.Abstractions/Helpers/DictionaryExtensions.cs +++ b/src/OrchardCore/OrchardCore.Workflows.Abstractions/Helpers/DictionaryExtensions.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Text.Json; namespace OrchardCore.Workflows.Helpers { @@ -25,14 +24,7 @@ public static TValue GetValue(this IDictionary dictionar return default; } - if (value is not TValue castedValue) - { - var json = JConvert.SerializeObject(value); - - return JConvert.DeserializeObject(json); - } - - return castedValue; + return (TValue)value; } /// From 33eb41368567e5ecc67e69df16f4234061037a76 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 3 Jun 2024 11:04:32 -0700 Subject: [PATCH 2/2] rename --- .../Workflows/Activities/ContentActivity.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/ContentActivity.cs b/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/ContentActivity.cs index efbdff7d968..ecf1874f896 100644 --- a/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/ContentActivity.cs +++ b/src/OrchardCore.Modules/OrchardCore.Contents/Workflows/Activities/ContentActivity.cs @@ -79,19 +79,19 @@ public override async Task OnWorkflowRestartingAsync(WorkflowExecutionContext wo if (workflowContext.Input.TryGetValue(ContentEventConstants.ContentEventInputKey, out var contentEvent)) { - if (contentEvent is not JsonObject er) + if (contentEvent is not JsonObject jsonObject) { - er = []; + jsonObject = []; if (contentEvent is Dictionary items) { foreach (var item in items) { - er[item.Key] = JsonSerializer.SerializeToNode(item.Value); + jsonObject[item.Key] = JsonSerializer.SerializeToNode(item.Value); } } } - var contentEventContext = er.ToObject(); + var contentEventContext = jsonObject.ToObject(); if (contentEventContext?.ContentItemVersionId != null) { @@ -105,19 +105,19 @@ public override async Task OnWorkflowRestartingAsync(WorkflowExecutionContext wo if (contentItem == null && workflowContext.Input.TryGetValue(ContentEventConstants.ContentItemInputKey, out var contentItemEvent)) { - if (contentItemEvent is not JsonObject er) + if (contentItemEvent is not JsonObject jsonObject) { - er = []; + jsonObject = []; if (contentEvent is Dictionary items) { foreach (var item in items) { - er[item.Key] = JsonSerializer.SerializeToNode(item.Value); + jsonObject[item.Key] = JsonSerializer.SerializeToNode(item.Value); } } } - var existingContentItem = er.ToObject(); + var existingContentItem = jsonObject.ToObject(); if (existingContentItem?.ContentItemId != null) {