From 1febb9a211680b22da10c07973e1b44502ab86f4 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 5 May 2021 13:13:05 +0200 Subject: [PATCH] Handle DateTime parsing for UTC strings --- src/core/Elsa.Core/Expressions/LiteralHandler.cs | 15 +++------------ .../Elsa.Core/Extensions/StringExtensions.cs | 16 ++++++++++++---- .../Expressions/LiteralHandlerTests.cs | 3 ++- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/core/Elsa.Core/Expressions/LiteralHandler.cs b/src/core/Elsa.Core/Expressions/LiteralHandler.cs index a4520deea..9b52aa202 100644 --- a/src/core/Elsa.Core/Expressions/LiteralHandler.cs +++ b/src/core/Elsa.Core/Expressions/LiteralHandler.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Globalization; using System.Threading; using System.Threading.Tasks; using Elsa.Services.Models; @@ -14,17 +15,7 @@ namespace Elsa.Expressions string expression, Type returnType, ActivityExecutionContext context, - CancellationToken cancellationToken) - { - if (returnType == typeof(string) || returnType == typeof(object)) - return Task.FromResult(expression); - - if (string.IsNullOrWhiteSpace(expression)) - return Task.FromResult((object?) null); - - var converter = TypeDescriptor.GetConverter(returnType); - var value = converter.CanConvertFrom(typeof(string)) ? converter.ConvertFrom(expression) : default; - return Task.FromResult(value)!; - } + CancellationToken cancellationToken) => + Task.FromResult(expression.Parse(returnType)); } } \ No newline at end of file diff --git a/src/core/Elsa.Core/Extensions/StringExtensions.cs b/src/core/Elsa.Core/Extensions/StringExtensions.cs index 65563bd2e..e86da59ec 100644 --- a/src/core/Elsa.Core/Extensions/StringExtensions.cs +++ b/src/core/Elsa.Core/Extensions/StringExtensions.cs @@ -1,19 +1,27 @@ using System; using System.ComponentModel; +using System.Globalization; namespace Elsa { public static class StringExtensions { - public static T Parse(this string value) => (T)value.Parse(typeof(T)); + public static T? Parse(this string value) => (T?)value.Parse(typeof(T)); - public static object Parse(this string value, Type targetType) + public static object? Parse(this string value, Type targetType) { - if (typeof(string) == targetType || typeof(object) == targetType || targetType == default) + if (typeof(string) == targetType || typeof(object) == targetType || targetType == default!) return value; + // Handling DateTime explicitly due to an issue with converting UTC strings to DateTime as outlined here: https://stackoverflow.com/questions/11130912/datetimeconverter-converting-from-utc-string + if(targetType == typeof(DateTime)) + return DateTime.Parse(value, null, DateTimeStyles.RoundtripKind); + + if (string.IsNullOrWhiteSpace(value)) + return null!; + var converter = TypeDescriptor.GetConverter(targetType); - return converter.ConvertFromString(value)!; + return converter.CanConvertFrom(typeof(string)) ? converter.ConvertFrom(value) : default; } } } \ No newline at end of file diff --git a/test/unit/Elsa.UnitTests/Expressions/LiteralHandlerTests.cs b/test/unit/Elsa.UnitTests/Expressions/LiteralHandlerTests.cs index bc2e7e341..415c6837c 100644 --- a/test/unit/Elsa.UnitTests/Expressions/LiteralHandlerTests.cs +++ b/test/unit/Elsa.UnitTests/Expressions/LiteralHandlerTests.cs @@ -57,7 +57,8 @@ namespace Elsa.Expressions [StubActivityExecutionContext] ActivityExecutionContext context) { var result = await sut.EvaluateAsync("2001-03-04T12:45:32Z", typeof(DateTime), context, default); - Assert.Equal(new DateTime(2001, 03, 04, 12, 45, 32, DateTimeKind.Utc), result); + var expected = new DateTime(2001, 03, 04, 12, 45, 32, DateTimeKind.Utc); + Assert.Equal(expected, result); } } } \ No newline at end of file