-
Notifications
You must be signed in to change notification settings - Fork 524
Query: Fixes LINQ to use custom serializer to serialize constant values in Query #3406
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
Merged
ealsur
merged 17 commits into
master
from
users/leminh/GetItemLinqQuerableSerializerFix
Oct 13, 2022
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e360dca
Add custom serializer to SqlObjectCreate argument to use when seriali…
e186f37
Provide serializer fix
b1eae2a
Clean up code change
13da7af
more clean up, removing the ToString from SqlObjectCreate fix
e6e2a1d
undo some old changes that no longer apply
46f4ff9
revert accidental change
d47b8ba
Address code reivew
022666b
Merge branch 'master' into users/leminh/GetItemLinqQuerableSerializerFix
leminh98 4bc1673
fix some unused var
6957e0f
Merge branch 'users/leminh/GetItemLinqQuerableSerializerFix' of https…
e320d7d
remove gateway policy
663ea3f
Change name for the Options property
8e3329c
contract change
cf12416
Merge branch 'master' into users/leminh/GetItemLinqQuerableSerializerFix
leminh98 4359ec7
change field to be internal
233bbde
Merge branch 'master' into users/leminh/GetItemLinqQuerableSerializerFix
leminh98 e551fce
Merge branch 'master' into users/leminh/GetItemLinqQuerableSerializerFix
ealsur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
48 changes: 48 additions & 0 deletions
48
...neTest/TestBaseline/LinqTranslationWithCustomSerializerBaseline.TestMemberInitializer.xml
This file contains hidden or 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,48 @@ | ||
| <Results> | ||
| <Result> | ||
| <Input> | ||
| <Description><![CDATA[Filter w/ DataObject initializer with constant value]]></Description> | ||
| <Expression><![CDATA[query.Where(doc => (doc == new DataObject() {NumericField = 12, StringField = "12"}))]]></Expression> | ||
| </Input> | ||
| <Output> | ||
| <SqlQuery><![CDATA[ | ||
| SELECT VALUE root | ||
| FROM root | ||
| WHERE (root = {"number": 12, "String_value": "12", "id": null, "Pk": null})]]></SqlQuery> | ||
| </Output> | ||
| </Result> | ||
| <Result> | ||
| <Input> | ||
| <Description><![CDATA[Select w/ DataObject initializer]]></Description> | ||
| <Expression><![CDATA[query.Select(doc => new DataObject() {NumericField = 12, StringField = "12"})]]></Expression> | ||
| </Input> | ||
| <Output> | ||
| <SqlQuery><![CDATA[ | ||
| SELECT VALUE {"number": 12, "String_value": "12", "id": null, "Pk": null} | ||
| FROM root]]></SqlQuery> | ||
| </Output> | ||
| </Result> | ||
| <Result> | ||
| <Input> | ||
| <Description><![CDATA[Deeper than top level reference]]></Description> | ||
| <Expression><![CDATA[query.Select(doc => IIF((doc.NumericField > 12), new DataObject() {NumericField = 12, StringField = "12"}, new DataObject() {NumericField = 12, StringField = "12"}))]]></Expression> | ||
| </Input> | ||
| <Output> | ||
| <SqlQuery><![CDATA[ | ||
| SELECT VALUE ((root["NumericField"] > 12) ? {"number": 12, "String_value": "12", "id": null, "Pk": null} : {"number": 12, "String_value": "12", "id": null, "Pk": null}) | ||
| FROM root]]></SqlQuery> | ||
| </Output> | ||
| </Result> | ||
| <Result> | ||
| <Input> | ||
| <Description><![CDATA[Filter w/ DataObject initializer with member initialization]]></Description> | ||
| <Expression><![CDATA[query.Where(doc => (doc == new DataObject() {NumericField = doc.NumericField, StringField = doc.StringField})).Select(b => "A")]]></Expression> | ||
| </Input> | ||
| <Output> | ||
| <SqlQuery><![CDATA[ | ||
| SELECT VALUE "A" | ||
| FROM root | ||
| WHERE (root = {"NumericField": root["NumericField"], "StringField": root["StringField"]})]]></SqlQuery> | ||
| </Output> | ||
| </Result> | ||
| </Results> |
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...t.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/LinqTranslationBaselineTests.cs
This file contains hidden or 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
157 changes: 157 additions & 0 deletions
157
...tests/Microsoft.Azure.Cosmos.EmulatorTests/LinqTranslationWithCustomSerializerBaseline.cs
This file contains hidden or 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,157 @@ | ||
| //----------------------------------------------------------------------- | ||
| // <copyright file="LinqTranslationWithCustomSerializerBaseline.cs" company="Microsoft Corporation"> | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // </copyright> | ||
| //----------------------------------------------------------------------- | ||
| namespace Microsoft.Azure.Cosmos.Services.Management.Tests.LinqProviderTests | ||
| { | ||
| using BaselineTest; | ||
| using Microsoft.Azure.Cosmos.Linq; | ||
| using Microsoft.Azure.Cosmos.Spatial; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using System.Linq.Dynamic; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using Microsoft.Azure.Documents; | ||
| using Microsoft.Azure.Cosmos.SDK.EmulatorTests; | ||
| using System.Threading.Tasks; | ||
| using global::Azure.Core.Serialization; | ||
| using System.IO; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| [Microsoft.Azure.Cosmos.SDK.EmulatorTests.TestClass] | ||
| public class LinqTranslationWithCustomSerializerBaseline : BaselineTests<LinqTestInput, LinqTestOutput> | ||
| { | ||
| private static CosmosClient cosmosClient; | ||
| private static Cosmos.Database testDb; | ||
| private static Container testContainer; | ||
leminh98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [ClassInitialize] | ||
| public async static Task Initialize(TestContext textContext) | ||
| { | ||
| cosmosClient = TestCommon.CreateCosmosClient((cosmosClientBuilder) | ||
leminh98 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| => cosmosClientBuilder.WithCustomSerializer(new SystemTextJsonSerializer(new JsonSerializerOptions())).WithConnectionModeGateway()); | ||
|
|
||
| string dbName = $"{nameof(LinqTranslationBaselineTests)}-{Guid.NewGuid().ToString("N")}"; | ||
| testDb = await cosmosClient.CreateDatabaseAsync(dbName); | ||
| } | ||
|
|
||
| [ClassCleanup] | ||
| public async static Task CleanUp() | ||
| { | ||
| if (testDb != null) | ||
| { | ||
| await testDb.DeleteStreamAsync(); | ||
| } | ||
| } | ||
|
|
||
| [TestInitialize] | ||
| public async Task TestInitialize() | ||
| { | ||
| testContainer = await testDb.CreateContainerAsync(new ContainerProperties(id: Guid.NewGuid().ToString(), partitionKeyPath: "/Pk")); | ||
| } | ||
|
|
||
| [TestCleanup] | ||
| public async Task TestCleanUp() | ||
| { | ||
| await testContainer.DeleteContainerStreamAsync(); | ||
| } | ||
|
|
||
| // Custom serializer that uses System.Text.Json.JsonSerializer instead of NewtonsoftJson.JsonSerializer | ||
| private class SystemTextJsonSerializer : CosmosSerializer | ||
| { | ||
| private readonly JsonObjectSerializer systemTextJsonSerializer; | ||
|
|
||
| public SystemTextJsonSerializer(JsonSerializerOptions jsonSerializerOptions) | ||
| { | ||
| this.systemTextJsonSerializer = new JsonObjectSerializer(jsonSerializerOptions); | ||
| } | ||
|
|
||
| public override T FromStream<T>(Stream stream) | ||
| { | ||
| if (stream == null) | ||
| throw new ArgumentNullException(nameof(stream)); | ||
|
|
||
| using (stream) | ||
| { | ||
| if (stream.CanSeek && stream.Length == 0) | ||
| { | ||
| return default; | ||
| } | ||
|
|
||
| if (typeof(Stream).IsAssignableFrom(typeof(T))) | ||
| { | ||
| return (T)(object)stream; | ||
| } | ||
|
|
||
| return (T)this.systemTextJsonSerializer.Deserialize(stream, typeof(T), default); | ||
| } | ||
| } | ||
|
|
||
| public override Stream ToStream<T>(T input) | ||
| { | ||
| MemoryStream streamPayload = new MemoryStream(); | ||
| this.systemTextJsonSerializer.Serialize(streamPayload, input, typeof(T), default); | ||
| streamPayload.Position = 0; | ||
| return streamPayload; | ||
| } | ||
| } | ||
|
|
||
| internal class DataObject : LinqTestObject | ||
| { | ||
| [JsonPropertyName("number")] | ||
| public double NumericField { get; set; } | ||
|
|
||
| [JsonPropertyName("String_value")] | ||
| public string StringField { get; set; } | ||
|
|
||
| [JsonPropertyName("id")] | ||
| public string Id { get; set; } | ||
|
|
||
| [JsonPropertyName("Pk")] | ||
| public string Pk { get; set; } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestMemberInitializer() | ||
| { | ||
| const int Records = 100; | ||
| const int NumAbsMax = 500; | ||
| const int MaxStringLength = 100; | ||
| DataObject createDataObj(Random random) | ||
| { | ||
| DataObject obj = new DataObject | ||
| { | ||
| NumericField = random.Next(NumAbsMax * 2) - NumAbsMax, | ||
| StringField = LinqTestsCommon.RandomString(random, random.Next(MaxStringLength)), | ||
| Id = Guid.NewGuid().ToString(), | ||
| Pk = "Test" | ||
| }; | ||
| return obj; | ||
| } | ||
| Func<bool, IQueryable<DataObject>> getQuery = LinqTestsCommon.GenerateTestCosmosData(createDataObj, Records, testContainer); | ||
|
|
||
| List<LinqTestInput> inputs = new List<LinqTestInput> | ||
| { | ||
| new LinqTestInput("Filter w/ DataObject initializer with constant value", b => getQuery(b).Where(doc => doc == new DataObject() { NumericField = 12, StringField = "12" })), | ||
| new LinqTestInput("Select w/ DataObject initializer", b => getQuery(b).Select(doc => new DataObject() { NumericField = 12, StringField = "12" })), | ||
| new LinqTestInput("Deeper than top level reference", b => getQuery(b).Select(doc => doc.NumericField > 12 ? new DataObject() { NumericField = 12, StringField = "12" } : new DataObject() { NumericField = 12, StringField = "12" })), | ||
|
|
||
|
|
||
| // Negative test case: serializing only field name using custom serializer not currently supported | ||
| new LinqTestInput("Filter w/ DataObject initializer with member initialization", b => getQuery(b).Where(doc => doc == new DataObject() { NumericField = doc.NumericField, StringField = doc.StringField }).Select(b => "A")) | ||
| }; | ||
| this.ExecuteTestSuite(inputs); | ||
| } | ||
|
|
||
|
|
||
| public override LinqTestOutput ExecuteTest(LinqTestInput input) | ||
| { | ||
| return LinqTestsCommon.ExecuteTest(input); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.