Skip to content

Commit 08c2b0b

Browse files
authored
Fix deserialization of DateTime types and allow type coercion between DateTime and DateTimeOffset peroperties (Azure#25804)
* Allow type coercion between DateTime and DateTimeOffset peroperties
1 parent 380316d commit 08c2b0b

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

sdk/tables/Azure.Data.Tables/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking Changes
88

99
### Bugs Fixed
10+
- Fixed a an issue when using `TableEntity.GetDateTime` that resulted in an `InvalidOperationException` exception. ([#25323](https://github.com/Azure/azure-sdk-for-net/issues/25323))
1011

1112
### Other Changes
1213

sdk/tables/Azure.Data.Tables/src/TableEntity.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,19 @@ private object GetValue(string key, Type type = null)
237237
if (type != null)
238238
{
239239
var valueType = value.GetType();
240-
if (type == typeof(BinaryData) && valueType == typeof(byte[]))
240+
if (type == typeof(DateTime?) && valueType == typeof(DateTimeOffset))
241+
{
242+
return ((DateTimeOffset)value).UtcDateTime;
243+
}
244+
if (type == typeof(DateTimeOffset?) && valueType == typeof(DateTime))
241245
{
242-
value = new BinaryData(value);
246+
return new DateTimeOffset((DateTime)value);
243247
}
244-
else
248+
if (type == typeof(BinaryData) && valueType == typeof(byte[]))
245249
{
246-
EnforceType(type, valueType);
250+
return new BinaryData(value);
247251
}
252+
EnforceType(type, valueType);
248253
}
249254

250255
return value;

sdk/tables/Azure.Data.Tables/tests/TableEntityTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,28 @@ public void TypeCoercionForNumericTypes()
246246
Assert.That(entity["Foo3"], Is.EqualTo(now));
247247
Assert.That(entity["Foo3"] is DateTime);
248248
}
249+
250+
[Test]
251+
public void TypeCoercionForDateTimeTypes()
252+
{
253+
var entity = new TableEntity("partition", "row");
254+
var offsetNow = DateTimeOffset.UtcNow;
255+
var dateNow = offsetNow.UtcDateTime;
256+
257+
// Initialize a property to an DateTimeOffset value
258+
entity["DTOffset"] = offsetNow;
259+
Assert.That(entity["DTOffset"] is DateTimeOffset);
260+
Assert.That(entity["DTOffset"], Is.EqualTo(offsetNow));
261+
Assert.That(entity.GetDateTimeOffset("DTOffset"), Is.EqualTo(offsetNow));
262+
Assert.That(entity.GetDateTime("DTOffset"), Is.EqualTo(dateNow));
263+
264+
// Initialize a property to an DateTime value
265+
entity["DT"] = dateNow;
266+
Assert.AreEqual(typeof(DateTime), entity["DT"].GetType());
267+
DateTimeOffset dtoffset = (DateTime)entity["DT"];
268+
Assert.That(entity["DT"], Is.EqualTo(dateNow));
269+
Assert.That(entity.GetDateTime("DT"), Is.EqualTo(dateNow));
270+
Assert.That(entity.GetDateTimeOffset("DT"), Is.EqualTo(offsetNow));
271+
}
249272
}
250273
}

0 commit comments

Comments
 (0)