From a3f40c95f77b7b6366c030dc8c3133a27b912083 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 17 Sep 2021 10:53:23 +0200 Subject: [PATCH] Fix type cast issue due to JSON serialization of enums When the workflow instance gets persisted in the DB, activity enum properties get stored as an integer (Int64), which cannot be cast to the enum. This change fixes that by performing a conversion instead of a direct cast. Fixes #1515 --- src/core/Elsa.Abstractions/Extensions/ObjectConverter.cs | 9 ++++++--- .../Services/Models/WorkflowExecutionContext.cs | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/core/Elsa.Abstractions/Extensions/ObjectConverter.cs b/src/core/Elsa.Abstractions/Extensions/ObjectConverter.cs index fca1f74f3..738227c82 100644 --- a/src/core/Elsa.Abstractions/Extensions/ObjectConverter.cs +++ b/src/core/Elsa.Abstractions/Extensions/ObjectConverter.cs @@ -15,13 +15,17 @@ namespace Elsa { if (value == null) return default!; + + var sourceType = value.GetType(); + + if (sourceType == targetType) + return value; var underlyingTargetType = Nullable.GetUnderlyingType(targetType) ?? targetType; if (targetType == typeof(object)) return value; - - var sourceType = value.GetType(); + var underlyingSourceType = Nullable.GetUnderlyingType(sourceType) ?? sourceType; if (underlyingSourceType == underlyingTargetType) @@ -63,7 +67,6 @@ namespace Elsa { throw new TypeConversionException($"Failed to convert an object of type {sourceType} to {underlyingTargetType}", value, underlyingTargetType, e); } - } } } \ No newline at end of file diff --git a/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs b/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs index ea3940768..4ce6d2e58 100644 --- a/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs +++ b/src/core/Elsa.Abstractions/Services/Models/WorkflowExecutionContext.cs @@ -230,7 +230,11 @@ namespace Elsa.Services.Models return await GetActivityPropertyAsync(activityBlueprint, propertyName, cancellationToken); } - public async Task GetActivityPropertyAsync(IActivityBlueprint activityBlueprint, string propertyName, CancellationToken cancellationToken = default) => (T?)await GetActivityPropertyAsync(activityBlueprint, propertyName, cancellationToken); + public async Task GetActivityPropertyAsync(IActivityBlueprint activityBlueprint, string propertyName, CancellationToken cancellationToken = default) + { + var value = await GetActivityPropertyAsync(activityBlueprint, propertyName, cancellationToken); + return value.ConvertTo(); + } public async Task GetActivityPropertyAsync(IActivityBlueprint activityBlueprint, string propertyName, CancellationToken cancellationToken = default) {