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

[rel/3.8] Fix serializing special characters in Jsonite #5125

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,10 @@ private void WriteString(string text)
writer.Write('t');
break;
default:
ArgumentGuard.Ensure(c >= ' ', nameof(text), $"Invalid control character '{EscapeChar(c)}' found in string");
// Intentionally diverging from the upstream code from xoofx/jsonite. See https://github.com/microsoft/testfx/issues/5120

if (IsHighSurrogate(c) || IsLowSurrogate(c))
// Also, https://datatracker.ietf.org/doc/html/rfc4627#section-2.5
if (c < ' ' || IsHighSurrogate(c) || IsLowSurrogate(c))
{
writer.Write('\\');
writer.Write('u');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !NETCOREAPP

namespace Microsoft.Testing.Platform.UnitTests;

[TestClass]
Expand All @@ -9,11 +11,48 @@ public sealed class JsoniteTests
[TestMethod]
public void Serialize_DateTimeOffset()
{
#if !NETCOREAPP
string actual = Jsonite.Json.Serialize(new DateTimeOffset(2023, 01, 01, 01, 01, 01, 01, TimeSpan.Zero));

// Assert
Assert.AreEqual("2023-01-01T01:01:01.0010000+00:00", actual.Trim('"'));
#endif
}

[TestMethod]
public void Serialize_SpecialCharacters()
{
// This test is testing if we can serialize the range 0x0000 - 0x001FF correctly, this range contains special characters like NUL.
// This is a fix for Jsonite, which throws when such characters are found in a string (but does not fail when we provide them as character).
List<Exception> errors = new();

// This could be converted to Data source, but this way we have more control about where in the result message the
// special characters will be (hopefully nowhere) so in case of failure, we can still serialize the message to IDE
// even if the serializer does not support special characters.
foreach (char character in Enumerable.Range(0x0000, 0x001F).Select(v => (char)v))
{
// Convert the char to string, otherwise there is no failure.
string text = $"{character}";

// Serialize text via Jsonite, this is where the error used to happen.
string jsoniteText = Jsonite.Json.Serialize(text);

// Make sure we can deserialize messages we produced, to know the Jsonite code is handling all the cases.
string? deserializeJsoniteViaJsonite = (string)Jsonite.Json.Deserialize(jsoniteText);

try
{
Assert.AreEqual(text, deserializeJsoniteViaJsonite);
}
catch (Exception ex)
{
errors.Add(ex);
}
}

if (errors.Count > 0)
{
throw new Exception(string.Join(Environment.NewLine, errors));
}
}
}

#endif
Loading