Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LINQ: Fixes preserve DateTime.Kind when passing value to custom JsonConverter #3224

Merged
merged 13 commits into from
Jun 6, 2022
Prev Previous commit
Next Next commit
Update test to not use timezones
  • Loading branch information
ccurrens committed Jun 3, 2022
commit cc2f590ef3077fb833d725e100d405d15d8c462a
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
<Result>
<Input>
<Description><![CDATA[IsoDateTimeConverter LocalTime = filter]]></Description>
<Expression><![CDATA[query.Where(doc => (doc.IsoTime == new DateTime(2016, 9, 13, 0, 0, 0, Local)))]]></Expression>
<Expression><![CDATA[query.Where(doc => (doc.IsoDateOnly == new DateTime(2016, 9, 13, 0, 0, 0, Local)))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE root
FROM root
WHERE (root["IsoTime"] = "2016-09-13T00:00:00-04:00")]]></SqlQuery>
WHERE (root["IsoDateOnly"] = "2016-09-13")]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[IsoDateTimeConverter UniversalTime = filter]]></Description>
<Expression><![CDATA[query.Where(doc => (doc.IsoTime == new DateTime(2016, 9, 13, 0, 0, 0, Utc)))]]></Expression>
<Expression><![CDATA[query.Where(doc => (doc.IsoDateOnly == new DateTime(2016, 9, 13, 0, 0, 0, Utc)))]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE root
FROM root
WHERE (root["IsoTime"] = "2016-09-13T00:00:00Z")]]></SqlQuery>
WHERE (root["IsoDateOnly"] = "2016-09-13")]]></SqlQuery>
</Output>
</Result>
</Results>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.Azure.Cosmos.Services.Management.Tests.LinqProviderTests
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json.Converters;
using BaselineTest;
Expand Down Expand Up @@ -134,6 +135,9 @@ internal class DataObject : LinqTestObject
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime IsoTime;

[JsonConverter(typeof(DateJsonConverter))]
public DateTime IsoDateOnly;

// This field should serialize as ISO Date
// as this is the default DateTimeConverter
// used by Newtonsoft
Expand All @@ -145,6 +149,21 @@ internal class DataObject : LinqTestObject
public string Pk;
}

class DateJsonConverter : IsoDateTimeConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is DateTime dateTime)
{
writer.WriteValue(dateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
}
else
{
base.WriteJson(writer, value, serializer);
}
}
}

internal class AmbientContextObject
{
public double FieldAccess;
Expand Down Expand Up @@ -358,7 +377,7 @@ public void TestDateTimeJsonConverterTimezones()
Func<Random, DataObject> createDataObj = (random) =>
{
DataObject obj = new() {
IsoTime = LinqTestsCommon.RandomDateTime(random, midDateTime),
IsoDateOnly = LinqTestsCommon.RandomDateTime(random, midDateTime),
Id = Guid.NewGuid().ToString(),
Pk = "Test"
};
Expand All @@ -368,8 +387,8 @@ public void TestDateTimeJsonConverterTimezones()

List<LinqTestInput> inputs = new()
{
new LinqTestInput("IsoDateTimeConverter LocalTime = filter", b => getQuery(b).Where(doc => doc.IsoTime == new DateTime(2016, 9, 13, 0, 0, 0, DateTimeKind.Local))),
new LinqTestInput("IsoDateTimeConverter UniversalTime = filter", b => getQuery(b).Where(doc => doc.IsoTime == new DateTime(2016, 9, 13, 0, 0, 0, DateTimeKind.Utc))),
new LinqTestInput("IsoDateTimeConverter LocalTime = filter", b => getQuery(b).Where(doc => doc.IsoDateOnly == new DateTime(2016, 9, 13, 0, 0, 0, DateTimeKind.Local))),
new LinqTestInput("IsoDateTimeConverter UniversalTime = filter", b => getQuery(b).Where(doc => doc.IsoDateOnly == new DateTime(2016, 9, 13, 0, 0, 0, DateTimeKind.Utc))),
};
this.ExecuteTestSuite(inputs);
}
Expand Down