Skip to content

Commit

Permalink
Make Json serializer less strict on JSON encoding (#2714)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucGenetier authored Oct 23, 2024
1 parent f907331 commit 05e865e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -25,7 +26,7 @@ public OpenApiJsonSerializer(IConvertToUTC utcConverter, bool schemaLessBody, Ca
: base(utcConverter, schemaLessBody)
{
_stream = new MemoryStream();
_writer = new Utf8JsonWriter(_stream, new JsonWriterOptions());
_writer = new Utf8JsonWriter(_stream, new JsonWriterOptions() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
_cancellationToken = cancellationToken;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ public void Office365Outlook_EnumFuncs()
// RespondToEventV2

[InlineData(
@"Office365Outlook.CalendarPostItem(""Calendar"", DateTime(2023, 12, 20, 17, 05, 0), DateTime(2023, 12, 20, 15, 15, 0), ""Event 17"", { ShowAs: ""Some Event"" }).Subject",
@"Office365Outlook.CalendarPostItem(""Calendar"", DateTime(2023, 12, 20, 17, 05, 0), DateTime(2023, 12, 20, 15, 15, 0), ""Event """"419"""" 😊"", { ShowAs: ""Some Event"" }).Subject",
"Event 17",
"POST:/apim/office365/3ea3b1e7f28d4c54a23a4dcbcae7de69/datasets/calendars/tables/Calendar/items",
@"{""End"":""2023-12-20T17:05:00.000Z"",""ShowAs"":""Some Event"",""Start"":""2023-12-20T15:15:00.000Z"",""Subject"":""Event 17""}",
"POST:/apim/office365/3ea3b1e7f28d4c54a23a4dcbcae7de69/datasets/calendars/tables/Calendar/items",
@"{""End"":""2023-12-20T17:05:00.000Z"",""ShowAs"":""Some Event"",""Start"":""2023-12-20T15:15:00.000Z"",""Subject"":""Event \""419\"" \uD83D\uDE0A""}",
"201:Response_O365Outlook_CalendarPostItem.json")]

[InlineData(
Expand Down Expand Up @@ -502,28 +502,28 @@ public void Office365Outlook_EnumFuncs()
@"Office365Outlook.ContactPostItem(""Contacts"", ""John Smith"", [""+1 (555) 457-987-1174""], { DisplayName: ""John II"", EmailAddresses: [ { Name: ""email1"", Address: ""john2@nowhere.com"" }, { Name: ""email2"", Address: ""john117zz@hotmail.se"" } ], CompanyName: ""Bank of Sweden""}).DisplayName",
"John II",
"POST:/apim/office365/3ea3b1e7f28d4c54a23a4dcbcae7de69/datasets/contacts/tables/Contacts/items",
@"{""DisplayName"":""John II"",""GivenName"":""John Smith"",""EmailAddresses"":[{""Name"":""email1"",""Address"":""john2@nowhere.com""},{""Name"":""email2"",""Address"":""john117zz@hotmail.se""}],""CompanyName"":""Bank of Sweden"",""HomePhones"":[""\u002B1 (555) 457-987-1174""]}",
@"{""DisplayName"":""John II"",""GivenName"":""John Smith"",""EmailAddresses"":[{""Name"":""email1"",""Address"":""john2@nowhere.com""},{""Name"":""email2"",""Address"":""john117zz@hotmail.se""}],""CompanyName"":""Bank of Sweden"",""HomePhones"":[""+1 (555) 457-987-1174""]}",
"201:Response_O365Outlook_ContactPostItem.json")]

[InlineData(
@"Office365Outlook.ContactPostItemV2(""Contacts"", ""John Smith 3"", [""+1 (555) 457-987-1174""], { displayName: ""John III"", emailAddresses: [ { name: ""email1"", address: ""john2@nowhere.com"" }, { name: ""email2"", address: ""john117zz@hotmail.se"" } ], companyName: ""Bank of Sweden""}).displayName",
"John III",
"POST:/apim/office365/3ea3b1e7f28d4c54a23a4dcbcae7de69/codeless/v1.0/me/contactFolders/Contacts/contacts",
@"{""displayName"":""John III"",""givenName"":""John Smith 3"",""emailAddresses"":[{""name"":""email1"",""address"":""john2@nowhere.com""},{""name"":""email2"",""address"":""john117zz@hotmail.se""}],""companyName"":""Bank of Sweden"",""homePhones"":[""\u002B1 (555) 457-987-1174""]}",
@"{""displayName"":""John III"",""givenName"":""John Smith 3"",""emailAddresses"":[{""name"":""email1"",""address"":""john2@nowhere.com""},{""name"":""email2"",""address"":""john117zz@hotmail.se""}],""companyName"":""Bank of Sweden"",""homePhones"":[""+1 (555) 457-987-1174""]}",
"201:Response_O365Outlook_ContactPostItemV2.json")]

[InlineData(
@"Office365Outlook.ContactPatchItem(""Contacts"", ""AAMkADZiMmZiZGEwLTIyZDYtNDA3ZC1hZjJkLTljYjgxNjQ5YjFkNwBGAAAAAAC1gTSkmbm5QLpPwj9qarJqBwDr1A2S1MsmTIW9552ybeHbAAAAAAEOAADr1A2S1MsmTIW9552ybeHbAABxF5LaAAA="", ""John Smith 2"", [""+1 (555) 457-987-1175""]).DisplayName",
"John Smith 2",
"PATCH:/apim/office365/3ea3b1e7f28d4c54a23a4dcbcae7de69/datasets/contacts/tables/Contacts/items/AAMkADZiMmZiZGEwLTIyZDYtNDA3ZC1hZjJkLTljYjgxNjQ5YjFkNwBGAAAAAAC1gTSkmbm5QLpPwj9qarJqBwDr1A2S1MsmTIW9552ybeHbAAAAAAEOAADr1A2S1MsmTIW9552ybeHbAABxF5LaAAA%253D",
@"{""GivenName"":""John Smith 2"",""HomePhones"":[""\u002B1 (555) 457-987-1175""]}",
@"{""GivenName"":""John Smith 2"",""HomePhones"":[""+1 (555) 457-987-1175""]}",
"200:Response_O365Outlook_ContactPatchItem.json")]

[InlineData(
@"Office365Outlook.ContactPatchItemV2(""Contacts"", ""AAMkADZiMmZiZGEwLTIyZDYtNDA3ZC1hZjJkLTljYjgxNjQ5YjFkNwBGAAAAAAC1gTSkmbm5QLpPwj9qarJqBwDr1A2S1MsmTIW9552ybeHbAAAAAAEOAADr1A2S1MsmTIW9552ybeHbAABxF5LaAAA="", ""John Smith IIIa"",[""+1 (555) 457-987-1177""]).displayName",
"John Smith IIIa",
"PATCH:/apim/office365/3ea3b1e7f28d4c54a23a4dcbcae7de69/codeless/v1.0/me/contactFolders/Contacts/contacts/AAMkADZiMmZiZGEwLTIyZDYtNDA3ZC1hZjJkLTljYjgxNjQ5YjFkNwBGAAAAAAC1gTSkmbm5QLpPwj9qarJqBwDr1A2S1MsmTIW9552ybeHbAAAAAAEOAADr1A2S1MsmTIW9552ybeHbAABxF5LaAAA%253D",
@"{""givenName"":""John Smith IIIa"",""homePhones"":[""\u002B1 (555) 457-987-1177""]}",
@"{""givenName"":""John Smith IIIa"",""homePhones"":[""+1 (555) 457-987-1177""]}",
"201:Response_O365Outlook_ContactPatchItemV2.json")]

[InlineData(
Expand Down Expand Up @@ -750,7 +750,7 @@ public void Office365Outlook_EnumFuncs()
@"Office365Outlook.V4CalendarPostItem(""Calendar"", ""Event 32"", DateTime(2023, 12, 27, 15, 10, 59, 117.594), DateTime(2023, 12, 27, 16, 22, 3, 902.111),""(UTC+09:30) Darwin"").startWithTimeZone",
"DATETIME:2023-12-27T15:10:59.117+09:30",
"POST:/apim/office365/3ea3b1e7f28d4c54a23a4dcbcae7de69/datasets/calendars/v4/tables/Calendar/items",
@"{""subject"":""Event 32"",""start"":""2023-12-27T15:10:59.117"",""end"":""2023-12-27T16:22:03.902"",""timeZone"":""(UTC\u002B09:30) Darwin""}",
@"{""subject"":""Event 32"",""start"":""2023-12-27T15:10:59.117"",""end"":""2023-12-27T16:22:03.902"",""timeZone"":""(UTC+09:30) Darwin""}",
"201:Response_O365Outlook_V4CalendarPostItem.json")]

[InlineData(
Expand All @@ -771,7 +771,7 @@ public void Office365Outlook_EnumFuncs()
@"Office365Outlook.V4CalendarPatchItem(""Calendar"", ""AAMkADZiMmZiZGEwLTIyZDYtNDA3ZC1hZjJkLTljYjgxNjQ5YjFkNwBGAAAAAAC1gTSkmbm5QLpPwj9qarJqBwDr1A2S1MsmTIW9552ybeHbAAAAAAENAADr1A2S1MsmTIW9552ybeHbAAB1rhBMAAA="", ""Event 32a"", DateTime(2023, 12, 27, 15, 10, 59, 117.594), DateTime(2023, 12, 27, 16, 22, 3, 902.111),""(UTC+09:30) Darwin"").subject",
"Event 32a",
"PATCH:/apim/office365/3ea3b1e7f28d4c54a23a4dcbcae7de69/datasets/calendars/v4/tables/Calendar/items/AAMkADZiMmZiZGEwLTIyZDYtNDA3ZC1hZjJkLTljYjgxNjQ5YjFkNwBGAAAAAAC1gTSkmbm5QLpPwj9qarJqBwDr1A2S1MsmTIW9552ybeHbAAAAAAENAADr1A2S1MsmTIW9552ybeHbAAB1rhBMAAA%253D",
@"{""subject"":""Event 32a"",""start"":""2023-12-27T15:10:59.117"",""end"":""2023-12-27T16:22:03.902"",""timeZone"":""(UTC\u002B09:30) Darwin""}",
@"{""subject"":""Event 32a"",""start"":""2023-12-27T15:10:59.117"",""end"":""2023-12-27T16:22:03.902"",""timeZone"":""(UTC+09:30) Darwin""}",
"Response_O365Outlook_V4CalendarPatchItem.json")]
public async Task Office365Outlook_Functions(string expr, string expectedResult, string xUrls, string xBodies, string expectedFiles, string extra = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task JsonSerializer_EscapedKey()
["a\\b\"c\""] = (SchemaInteger, FormulaValue.New(1))
});

Assert.Equal(@"{""a\\b\u0022c\u0022"":1}", str);
Assert.Equal(@"{""a\\b\""c\"""":1}", str);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ public async Task Office365Outlook_V4CalendarPostItem(int id, string expr)
x-ms-request-url: /apim/office365/785da26033fe4f3f8604273d25f209d5/datasets/calendars/v4/tables/Calendar/items
x-ms-user-agent: PowerFx/{version}
[content-header] Content-Type: application/json; charset=utf-8
[body] {{""subject"":""Subject"",""start"":""2023-06-02T{(id == 1 ? "00" : "11")}:00:00.000"",""end"":""2023-06-02T{(id == 1 ? "00:00" : "11:30")}:00.000"",""timeZone"":""(UTC\u002B01:00) Brussels, Copenhagen, Madrid, Paris""}}
[body] {{""subject"":""Subject"",""start"":""2023-06-02T{(id == 1 ? "00" : "11")}:00:00.000"",""end"":""2023-06-02T{(id == 1 ? "00:00" : "11:30")}:00.000"",""timeZone"":""(UTC+01:00) Brussels, Copenhagen, Madrid, Paris""}}
";

AssertEqual(expected, actual);
Expand Down Expand Up @@ -1441,7 +1441,7 @@ public async Task SQL_GetRelationships()
x-ms-request-url: /apim/sql/53f515b50c3e4925803ec1f0945e799f/v2/datasets/pfxdev-sql.database.windows.net,SampleDB/query/sql
x-ms-user-agent: PowerFx/{PowerPlatformConnectorClient.Version}
[content-header] Content-Type: application/json; charset=utf-8
[body] {{""query"":""SELECT fk.name AS FK_Name, \u0027[\u0027 \u002B sp.name \u002B \u0027].[\u0027 \u002B tp.name \u002B \u0027]\u0027 AS Parent_Table, cp.name AS Parent_Column, \u0027[\u0027 \u002B sr.name \u002B \u0027].[\u0027 \u002B tr.name \u002B \u0027]\u0027 AS Referenced_Table, cr.name AS Referenced_Column FROM sys.foreign_keys fk INNER JOIN sys.tables tp ON fk.parent_object_id = tp.object_id INNER JOIN sys.tables tr ON fk.referenced_object_id = tr.object_id INNER JOIN sys.schemas sp on tp.schema_id = sp.schema_id INNER JOIN sys.schemas sr on tr.schema_id = sr.schema_id INNER JOIN sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id INNER JOIN sys.columns cp ON fkc.parent_column_id = cp.column_id AND fkc.parent_object_id = cp.object_id INNER JOIN sys.columns cr ON fkc.referenced_column_id = cr.column_id AND fkc.referenced_object_id = cr.object_id WHERE \u0027[\u0027 \u002B sp.name \u002B \u0027].[\u0027 \u002B tp.name \u002B \u0027]\u0027 = \u0027[SalesLT].[Product]\u0027""}}
[body] {{""query"":""SELECT fk.name AS FK_Name, '[' + sp.name + '].[' + tp.name + ']' AS Parent_Table, cp.name AS Parent_Column, '[' + sr.name + '].[' + tr.name + ']' AS Referenced_Table, cr.name AS Referenced_Column FROM sys.foreign_keys fk INNER JOIN sys.tables tp ON fk.parent_object_id = tp.object_id INNER JOIN sys.tables tr ON fk.referenced_object_id = tr.object_id INNER JOIN sys.schemas sp on tp.schema_id = sp.schema_id INNER JOIN sys.schemas sr on tr.schema_id = sr.schema_id INNER JOIN sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id INNER JOIN sys.columns cp ON fkc.parent_column_id = cp.column_id AND fkc.parent_object_id = cp.object_id INNER JOIN sys.columns cr ON fkc.referenced_column_id = cr.column_id AND fkc.referenced_object_id = cr.object_id WHERE '[' + sp.name + '].[' + tp.name + ']' = '[SalesLT].[Product]'""}}
";
Assert.Equal(expected, testConnector._log.ToString());
}
Expand Down

0 comments on commit 05e865e

Please sign in to comment.