-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LINQ: Fixes preserve DateTime.Kind when passing value to custom JsonC…
…onverter (#3224) * LINQ: preserve DateTime.Kind when passing to custom JsonConverter * Add named parameter * Add unit test * Update test * Remove unused namespaces * Fix nit * Add emulator test * Update test to not use timezones Co-authored-by: j82w <j82w@users.noreply.github.com> Co-authored-by: Matias Quaranta <ealsur@users.noreply.github.com>
- Loading branch information
1 parent
35c177b
commit dfa0b7c
Showing
5 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...lineTest/TestBaseline/LinqTranslationBaselineTests.TestDateTimeJsonConverterTimezones.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Results> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[IsoDateTimeConverter LocalTime = filter]]></Description> | ||
<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["IsoDateOnly"] = "2016-09-13")]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[IsoDateTimeConverter UniversalTime = filter]]></Description> | ||
<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["IsoDateOnly"] = "2016-09-13")]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
</Results> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...soft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Linq/CosmosLinqJsonConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Linq | ||
{ | ||
using System; | ||
using System.Globalization; | ||
using System.Linq.Expressions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
||
[TestClass] | ||
public class CosmosLinqJsonConverterTests | ||
{ | ||
[TestMethod] | ||
public void DateTimeKindIsPreservedTest() | ||
{ | ||
// Should work for UTC time | ||
DateTime utcDate = new DateTime(2022, 5, 26, 0, 0, 0, DateTimeKind.Utc); | ||
Expression<Func<TestDocument, bool>> expr = a => a.StartDate <= utcDate; | ||
string sql = SqlTranslator.TranslateExpression(expr.Body); | ||
Assert.AreEqual("(a[\"StartDate\"] <= \"2022-05-26\")", sql); | ||
|
||
// Should work for local time | ||
DateTime localDate = new DateTime(2022, 5, 26, 0, 0, 0, DateTimeKind.Local); | ||
expr = a => a.StartDate <= localDate; | ||
sql = SqlTranslator.TranslateExpression(expr.Body); | ||
Assert.AreEqual("(a[\"StartDate\"] <= \"2022-05-26\")", sql); | ||
} | ||
|
||
class TestDocument | ||
{ | ||
[JsonConverter(typeof(DateJsonConverter))] | ||
public DateTime StartDate { get; set; } | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} | ||
} | ||
} |