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
This commit is contained in:
Sipke Schoorstra 2021-09-17 10:53:23 +02:00
parent fbaa88688b
commit a3f40c95f7
2 changed files with 11 additions and 4 deletions

View file

@ -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);
}
}
}
}

View file

@ -230,7 +230,11 @@ namespace Elsa.Services.Models
return await GetActivityPropertyAsync<T>(activityBlueprint, propertyName, cancellationToken);
}
public async Task<T?> GetActivityPropertyAsync<T>(IActivityBlueprint activityBlueprint, string propertyName, CancellationToken cancellationToken = default) => (T?)await GetActivityPropertyAsync(activityBlueprint, propertyName, cancellationToken);
public async Task<T?> GetActivityPropertyAsync<T>(IActivityBlueprint activityBlueprint, string propertyName, CancellationToken cancellationToken = default)
{
var value = await GetActivityPropertyAsync(activityBlueprint, propertyName, cancellationToken);
return value.ConvertTo<T?>();
}
public async Task<object?> GetActivityPropertyAsync(IActivityBlueprint activityBlueprint, string propertyName, CancellationToken cancellationToken = default)
{