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

Fix a json serializer bug. #6722

Merged
merged 17 commits into from
Jan 8, 2024
Merged
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 @@ -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;
Expand All @@ -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
{
Expand All @@ -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));
Expand All @@ -52,6 +60,8 @@ public StreamingSession(RequestHandler receiver, TransportHandler sender, ILogge
_connectionCancellationToken = connectionCancellationToken;
}

private static JsonSerializer Serializer { get; }

public async Task<ReceiveResponse> SendRequestAsync(StreamingRequest request, CancellationToken cancellationToken)
{
if (request == null)
Expand Down Expand Up @@ -461,10 +471,16 @@ private static T DeserializeTo<T>(ReadOnlySequence<byte> payload)
mainPayload = payload.Slice(_utf8Bom.Length);
}

var reader = new Utf8JsonReader(mainPayload);
return System.Text.Json.JsonSerializer.Deserialize<T>(
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<T>(jsonReader);
}
}
}
}

private static void CreatePlaceholderStreams(Header header, List<IContentStream> placeholders, List<StreamDescription> streamInfo)
Expand Down
Loading