Skip to content

Commit 3ba59f2

Browse files
committed
CSHARP-2646: DateTimeOffsetSerializer should be forgiving of the actual numeric types it encounters during deserialization.
1 parent 1c8fae5 commit 3ba59f2

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/MongoDB.Bson/Serialization/Serializers/DateTimeOffsetSerializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public override DateTimeOffset Deserialize(BsonDeserializationContext context, B
105105
{
106106
case BsonType.Array:
107107
bsonReader.ReadStartArray();
108-
ticks = bsonReader.ReadInt64();
109-
offset = TimeSpan.FromMinutes(bsonReader.ReadInt32());
108+
ticks = _int64Serializer.Deserialize(context);
109+
offset = TimeSpan.FromMinutes(_int32Serializer.Deserialize(context));
110110
bsonReader.ReadEndArray();
111111
return new DateTimeOffset(ticks, offset);
112112

tests/MongoDB.Bson.Tests/Serialization/Serializers/DateTimeOffsetSerializerTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,34 @@ public void Deserialize_should_return_expected_result(string json, string expect
8787
result.Should().Be(DateTimeOffset.Parse(expectedResult));
8888
}
8989

90+
[Theory]
91+
[InlineData("{ x : [{ $numberDouble : '0' }, { $numberDouble : '0' }] }", "0001-01-01T00:00:00+00:00")]
92+
[InlineData("{ x : [{ $numberDouble : '621355968000000000' }, { $numberDouble : '0' }] }", "1970-01-01T00:00:00+00:00")]
93+
[InlineData("{ x : [{ $numberDouble : '621355968000000000' }, { $numberDouble : '60' }] }", "1970-01-01T00:00:00+01:00")]
94+
[InlineData("{ x : [{ $numberDouble : '621355968000000000' }, { $numberDouble : '-60' }] }", "1970-01-01T00:00:00-01:00")]
95+
[InlineData("{ x : { DateTime : 'ignored', Ticks : { $numberDouble : 0 }, Offset : { $numberDouble : '0' } } }", "0001-01-01T00:00:00Z")]
96+
[InlineData("{ x : { DateTime : 'ignored', Ticks : { $numberDouble : '621355968000000000' }, Offset : { $numberDouble : '0' } } }", "1970-01-01T00:00:00Z")]
97+
[InlineData("{ x : { DateTime : 'ignored', Ticks : { $numberDouble : '621355968000000000' }, Offset : { $numberDouble : '60' } } }", "1970-01-01T00:00:00+01:00")]
98+
[InlineData("{ x : { DateTime : 'ignored', Ticks : { $numberDouble : '621355968000000000' }, Offset : { $numberDouble : '-60' } } }", "1970-01-01T00:00:00-01:00")]
99+
public void Deserialize_should_be_forgiving_of_actual_numeric_types(string json, string expectedResult)
100+
{
101+
var x = DateTimeOffset.Parse(expectedResult);
102+
var m = BsonUtils.ToMillisecondsSinceEpoch(x.UtcDateTime);
103+
var subject = new DateTimeOffsetSerializer();
104+
105+
DateTimeOffset result;
106+
using (var reader = new JsonReader(json))
107+
{
108+
reader.ReadStartDocument();
109+
reader.ReadName("x");
110+
var context = BsonDeserializationContext.CreateRoot(reader);
111+
result = subject.Deserialize(context);
112+
reader.ReadEndDocument();
113+
}
114+
115+
result.Should().Be(DateTimeOffset.Parse(expectedResult));
116+
}
117+
90118
[Theory]
91119
[InlineData(BsonType.Array, "0001-01-01T00:00:00Z", "{ \"x\" : [{ \"$numberLong\" : \"0\" }, { \"$numberInt\" : \"0\" }] }")]
92120
[InlineData(BsonType.Array, "1970-01-01T00:00:00Z", "{ \"x\" : [{ \"$numberLong\" : \"621355968000000000\" }, { \"$numberInt\" : \"0\" }] }")]

0 commit comments

Comments
 (0)