Skip to content

Commit 0bdfc26

Browse files
russcamgmarz
authored andcommitted
Do not use IsoDateTimeConverter to handle DateTimeOffset (#2270)
Forward port of 4ea007c IsoDateTimeConverter deserializes a DateTimeOffset represented in an ISO8601 into a DateTimeOffset with a local offset. For example "1999-01-01T01:01:01.001+05:00" deserializes to a DateTimeOffset with a value of 01/01/1999 07:01:01 +11:00. The +11 offset is my local offset (Australia). The default deserialization for a DateTimeOffset not using IsoDateTimeConverter deserializes into a DateTimeOffset with an offset that reflects the one in the ISO8601 string representation. This seems more appropriate behaviour. Use InvariantCulture in the IsoDateTimeConverter when deserializing DateTime
1 parent a7ba8cc commit 0bdfc26

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

src/Nest/CommonAbstractions/SerializationBehavior/ElasticContractResolver.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
5+
using System.Globalization;
56
using System.Linq;
67
using System.Reflection;
78
using Elasticsearch.Net;
@@ -46,10 +47,8 @@ protected override JsonContract CreateContract(Type objectType)
4647
else if (o == typeof(ServerError))
4748
contract.Converter = new ServerErrorJsonConverter();
4849
else if (o == typeof(DateTime) ||
49-
o == typeof(DateTime?) ||
50-
o == typeof(DateTimeOffset) ||
51-
o == typeof(DateTimeOffset?))
52-
contract.Converter = new IsoDateTimeConverter();
50+
o == typeof(DateTime?))
51+
contract.Converter = new IsoDateTimeConverter { Culture = CultureInfo.InvariantCulture };
5352
else if (o == typeof(TimeSpan) ||
5453
o == typeof(TimeSpan?))
5554
contract.Converter = new TimeSpanConverter();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using Elasticsearch.Net;
5+
using FluentAssertions;
6+
using Nest;
7+
using Tests.Framework;
8+
9+
namespace Tests.Reproduce
10+
{
11+
public class DateSerialization
12+
{
13+
[U]
14+
public void ShouldRoundtripDateTimeAndDateTimeOffsetWithSameKindAndOffset()
15+
{
16+
var dates = new Dates
17+
{
18+
DateTimeUtcKind = new DateTime(2016,1,1,1,1,1,DateTimeKind.Utc),
19+
DateTimeOffset = new DateTimeOffset(1999, 1, 1, 1, 1, 1, 1, TimeSpan.FromHours(5))
20+
};
21+
22+
var client = new ElasticClient();
23+
var serializedDates = client.Serializer.SerializeToString(dates,SerializationFormatting.None);
24+
25+
serializedDates.Should()
26+
.Be("{\"dateTimeUtcKind\":\"2016-01-01T01:01:01Z\",\"dateTimeOffset\":\"1999-01-01T01:01:01.001+05:00\"}");
27+
28+
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(serializedDates)))
29+
{
30+
var deserializedDates = client.Serializer.Deserialize<Dates>(stream);
31+
32+
deserializedDates.DateTimeUtcKind.Should().Be(dates.DateTimeUtcKind);
33+
deserializedDates.DateTimeUtcKind.Kind.Should().Be(dates.DateTimeUtcKind.Kind);
34+
35+
deserializedDates.DateTimeOffset.Should().Be(dates.DateTimeOffset);
36+
deserializedDates.DateTimeOffset.Offset.Should().Be(dates.DateTimeOffset.Offset);
37+
deserializedDates.DateTimeOffset.Date.Kind.Should().Be(dates.DateTimeOffset.Date.Kind);
38+
}
39+
}
40+
41+
private class Dates
42+
{
43+
public DateTime DateTimeUtcKind { get; set; }
44+
public DateTimeOffset DateTimeOffset { get; set; }
45+
}
46+
}
47+
}

src/Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@
614614
<Compile Include="QueryDsl\MatchNoneQueryUsageTests.cs" />
615615
<Compile Include="QueryDsl\NotConditionlessWhen.cs" />
616616
<Compile Include="QueryDsl\Specialized\Percolate\PercolateQueryUsageTests.cs" />
617+
<Compile Include="Reproduce\DateSerialization.cs" />
617618
<Compile Include="Reproduce\GithubIssue2152.cs" />
618619
<Compile Include="Reproduce\GithubIssue2309.cs" />
619620
<Compile Include="Search\MultiSearch\MultiSearchApiTests.cs" />

0 commit comments

Comments
 (0)