From c374ed6eab561e3b5b5802974bbad8f2d10102f4 Mon Sep 17 00:00:00 2001 From: fangyangci <133664123+fangyangci@users.noreply.github.com> Date: Mon, 8 Jan 2024 22:12:01 +0800 Subject: [PATCH] Fix a json serializer bug. (#6722) * fixUSGovSingleTenant * Add UT * AseChannelValidation * Add UT * Rollback AuthTenant Property Name * The Ctor do contains the old ones, Add Ctor to ApiCompatBaseline * fix a coding mistake * fix json Serializer bug * 1 * fix setting --- .../Session/StreamingSession.cs | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/libraries/Microsoft.Bot.Connector.Streaming/Session/StreamingSession.cs b/libraries/Microsoft.Bot.Connector.Streaming/Session/StreamingSession.cs index c691b19183..d05289171d 100644 --- a/libraries/Microsoft.Bot.Connector.Streaming/Session/StreamingSession.cs +++ b/libraries/Microsoft.Bot.Connector.Streaming/Session/StreamingSession.cs @@ -9,7 +9,6 @@ using System.Linq; using System.Net; using System.Runtime.InteropServices; -using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Microsoft.Bot.Connector.Streaming.Payloads; @@ -19,6 +18,8 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Net.Http.Headers; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; namespace Microsoft.Bot.Connector.Streaming.Session { @@ -42,6 +43,13 @@ internal class StreamingSession private readonly object _receiveSync = new object(); + static StreamingSession() + { + var tmpSetting = SerializationSettings.DefaultSerializationSettings; + tmpSetting.NullValueHandling = NullValueHandling.Ignore; + Serializer = JsonSerializer.Create(tmpSetting); + } + public StreamingSession(RequestHandler receiver, TransportHandler sender, ILogger logger, CancellationToken connectionCancellationToken = default) { _receiver = receiver ?? throw new ArgumentNullException(nameof(receiver)); @@ -52,6 +60,8 @@ public StreamingSession(RequestHandler receiver, TransportHandler sender, ILogge _connectionCancellationToken = connectionCancellationToken; } + private static JsonSerializer Serializer { get; } + public async Task SendRequestAsync(StreamingRequest request, CancellationToken cancellationToken) { if (request == null) @@ -461,10 +471,16 @@ private static T DeserializeTo(ReadOnlySequence payload) mainPayload = payload.Slice(_utf8Bom.Length); } - var reader = new Utf8JsonReader(mainPayload); - return System.Text.Json.JsonSerializer.Deserialize( - ref reader, - new JsonSerializerOptions() { IgnoreNullValues = true, PropertyNameCaseInsensitive = true }); + using (var ms = new MemoryStream(mainPayload.ToArray())) + { + using (var sr = new StreamReader(ms)) + { + using (var jsonReader = new JsonTextReader(sr)) + { + return Serializer.Deserialize(jsonReader); + } + } + } } private static void CreatePlaceholderStreams(Header header, List placeholders, List streamInfo)