From 833861bf7458f0d3b13c37da7a36d9e6edad5662 Mon Sep 17 00:00:00 2001 From: AlekseyMartynov Date: Mon, 26 Apr 2021 14:14:34 +0300 Subject: [PATCH] Fix DateTime to DateTimeOffset conversion (#478) --- ...erExpressionCompilerTypeConversionTests.cs | 22 ++++++++++++++++++- net/DevExtreme.AspNet.Data/Utils.cs | 3 +++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/net/DevExtreme.AspNet.Data.Tests/FilterExpressionCompilerTypeConversionTests.cs b/net/DevExtreme.AspNet.Data.Tests/FilterExpressionCompilerTypeConversionTests.cs index c28812b3..bb1fbe6b 100644 --- a/net/DevExtreme.AspNet.Data.Tests/FilterExpressionCompilerTypeConversionTests.cs +++ b/net/DevExtreme.AspNet.Data.Tests/FilterExpressionCompilerTypeConversionTests.cs @@ -1,4 +1,6 @@ -using System; +using Newtonsoft.Json; +using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -223,6 +225,24 @@ public void StringFuncOnTimeSpan() { AssertEvaluation(obj, new[] { "NullableTime", "contains", "23" }); } + [Theory] + [InlineData(DateParseHandling.None)] + [InlineData(DateParseHandling.DateTime)] + [InlineData(DateParseHandling.DateTimeOffset)] + public void Issue477(DateParseHandling dateParseHandling) { + var date = new DateTimeOffset(2021, 1, 1, 0, 0, 0, TimeSpan.Zero); + var filterJSON = JsonConvert.SerializeObject(new object[] { "this", date }); + var deserializedFilter = JsonConvert.DeserializeObject(filterJSON, new JsonSerializerSettings { + DateParseHandling = dateParseHandling + }); + + var loadOptions = new SampleLoadOptions { + Filter = deserializedFilter + }; + + var loadResult = DataSourceLoader.Load(new[] { date }, loadOptions); + Assert.Single(loadResult.data); + } } } diff --git a/net/DevExtreme.AspNet.Data/Utils.cs b/net/DevExtreme.AspNet.Data/Utils.cs index b2c19002..e2c949c4 100644 --- a/net/DevExtreme.AspNet.Data/Utils.cs +++ b/net/DevExtreme.AspNet.Data/Utils.cs @@ -47,6 +47,9 @@ public static object ConvertClientValue(object value, Type type) { if(type == typeof(DateTime) && value is String) return DateTime.Parse((string)value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind); + if(type == typeof(DateTimeOffset) && value is DateTime date) + return new DateTimeOffset(date); + var converter = TypeDescriptor.GetConverter(type); if(converter != null && converter.CanConvertFrom(value.GetType())) return converter.ConvertFrom(null, CultureInfo.InvariantCulture, value);