From d39e1ed64ebb4a1f6e921a1f1f1c88b02f45904f Mon Sep 17 00:00:00 2001
From: Tanay Parikh <TanayParikh@users.noreply.github.com>
Date: Wed, 11 Aug 2021 14:59:29 -0700
Subject: [PATCH] Upgrade Newtonsoft to v13.0 (#35136)

For: dotnet/source-build#2358
---
 eng/Versions.props                                    |  2 +-
 .../Mvc.NewtonsoftJson/src/BsonTempDataSerializer.cs  |  8 ++++----
 src/Mvc/Mvc.NewtonsoftJson/src/JsonArrayPool.cs       |  2 +-
 .../src/NewtonsoftJsonInputFormatter.cs               |  2 +-
 .../Mvc.NewtonsoftJson/src/ProblemDetailsConverter.cs |  6 +++---
 .../Mvc.NewtonsoftJson/src/PublicAPI.Unshipped.txt    |  8 ++++----
 .../src/ValidationProblemDetailsConverter.cs          |  6 +++---
 .../Microsoft.AspNetCore.Razor.Language.Test.csproj   |  2 +-
 .../src/Protocol/NewtonsoftJsonHubProtocol.cs         |  4 ++--
 src/SignalR/common/Shared/JsonUtils.cs                | 11 ++++++++---
 10 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/eng/Versions.props b/eng/Versions.props
index 9bdab73d6e9..ad09f00887b 100644
--- a/eng/Versions.props
+++ b/eng/Versions.props
@@ -242,7 +242,7 @@
     <MoqVersion>4.10.0</MoqVersion>
     <MonoCecilVersion>0.11.2</MonoCecilVersion>
     <NewtonsoftJsonBsonVersion>1.0.2</NewtonsoftJsonBsonVersion>
-    <NewtonsoftJsonVersion>12.0.2</NewtonsoftJsonVersion>
+    <NewtonsoftJsonVersion>13.0.1</NewtonsoftJsonVersion>
     <NSwagApiDescriptionClientVersion>13.0.4</NSwagApiDescriptionClientVersion>
     <PhotinoNETVersion>1.1.6</PhotinoNETVersion>
     <PlaywrightSharpVersion>0.192.0</PlaywrightSharpVersion>
diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/BsonTempDataSerializer.cs b/src/Mvc/Mvc.NewtonsoftJson/src/BsonTempDataSerializer.cs
index 5674a4e0056..7623adda741 100644
--- a/src/Mvc/Mvc.NewtonsoftJson/src/BsonTempDataSerializer.cs
+++ b/src/Mvc/Mvc.NewtonsoftJson/src/BsonTempDataSerializer.cs
@@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Mvc.NewtonsoftJson
 
         public override IDictionary<string, object?> Deserialize(byte[] value)
         {
-            Dictionary<string, object?> tempDataDictionary;
+            Dictionary<string, object?>? tempDataDictionary;
 
             using (var memoryStream = new MemoryStream(value))
             using (var reader = new BsonDataReader(memoryStream))
@@ -222,14 +222,14 @@ namespace Microsoft.AspNetCore.Mvc.NewtonsoftJson
             return true;
         }
 
-        private static IList<TVal> ConvertArray<TVal>(JArray array)
+        private static IList<TVal?> ConvertArray<TVal>(JArray array)
         {
             return array.Values<TVal>().ToArray();
         }
 
-        private static IDictionary<string, TVal> ConvertDictionary<TVal>(JObject jObject)
+        private static IDictionary<string, TVal?> ConvertDictionary<TVal>(JObject jObject)
         {
-            var convertedDictionary = new Dictionary<string, TVal>(StringComparer.Ordinal);
+            var convertedDictionary = new Dictionary<string, TVal?>(StringComparer.Ordinal);
             foreach (var item in jObject)
             {
                 convertedDictionary.Add(item.Key, jObject.Value<TVal>(item.Key));
diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/JsonArrayPool.cs b/src/Mvc/Mvc.NewtonsoftJson/src/JsonArrayPool.cs
index 9d5a6a66671..d679b69fcb8 100644
--- a/src/Mvc/Mvc.NewtonsoftJson/src/JsonArrayPool.cs
+++ b/src/Mvc/Mvc.NewtonsoftJson/src/JsonArrayPool.cs
@@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Mvc.NewtonsoftJson
             return _inner.Rent(minimumLength);
         }
 
-        public void Return(T[] array)
+        public void Return(T[]? array)
         {
             if (array == null)
             {
diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs
index 55686f4fc33..13992f1bc41 100644
--- a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs
+++ b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs
@@ -165,7 +165,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
 
             var successful = true;
             Exception? exception = null;
-            object model;
+            object? model;
 
             using (var streamReader = context.ReaderFactory(readStream, encoding))
             {
diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/ProblemDetailsConverter.cs b/src/Mvc/Mvc.NewtonsoftJson/src/ProblemDetailsConverter.cs
index 420f19362a6..f2ddce498d7 100644
--- a/src/Mvc/Mvc.NewtonsoftJson/src/ProblemDetailsConverter.cs
+++ b/src/Mvc/Mvc.NewtonsoftJson/src/ProblemDetailsConverter.cs
@@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Mvc.NewtonsoftJson
         }
 
         /// <inheritdoc />
-        public override object? ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+        public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
         {
             var annotatedProblemDetails = serializer.Deserialize<AnnotatedProblemDetails>(reader);
             if (annotatedProblemDetails == null)
@@ -26,14 +26,14 @@ namespace Microsoft.AspNetCore.Mvc.NewtonsoftJson
                 return null;
             }
 
-            var problemDetails = (ProblemDetails)existingValue ?? new ProblemDetails();
+            var problemDetails = (ProblemDetails?)existingValue ?? new ProblemDetails();
             annotatedProblemDetails.CopyTo(problemDetails);
 
             return problemDetails;
         }
 
         /// <inheritdoc />
-        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+        public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
         {
             if (value == null)
             {
diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/PublicAPI.Unshipped.txt b/src/Mvc/Mvc.NewtonsoftJson/src/PublicAPI.Unshipped.txt
index 4029378de27..82880afd7ab 100644
--- a/src/Mvc/Mvc.NewtonsoftJson/src/PublicAPI.Unshipped.txt
+++ b/src/Mvc/Mvc.NewtonsoftJson/src/PublicAPI.Unshipped.txt
@@ -45,11 +45,11 @@ override Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.Write
 override Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonPatchInputFormatter.CanRead(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext! context) -> bool
 override Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonPatchInputFormatter.ReadRequestBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext! context, System.Text.Encoding! encoding) -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.Formatters.InputFormatterResult!>!
 override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ProblemDetailsConverter.CanConvert(System.Type! objectType) -> bool
-override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ProblemDetailsConverter.ReadJson(Newtonsoft.Json.JsonReader! reader, System.Type! objectType, object! existingValue, Newtonsoft.Json.JsonSerializer! serializer) -> object?
-override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ProblemDetailsConverter.WriteJson(Newtonsoft.Json.JsonWriter! writer, object! value, Newtonsoft.Json.JsonSerializer! serializer) -> void
+override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ProblemDetailsConverter.ReadJson(Newtonsoft.Json.JsonReader! reader, System.Type! objectType, object? existingValue, Newtonsoft.Json.JsonSerializer! serializer) -> object?
+override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ProblemDetailsConverter.WriteJson(Newtonsoft.Json.JsonWriter! writer, object? value, Newtonsoft.Json.JsonSerializer! serializer) -> void
 override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ValidationProblemDetailsConverter.CanConvert(System.Type! objectType) -> bool
-override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ValidationProblemDetailsConverter.ReadJson(Newtonsoft.Json.JsonReader! reader, System.Type! objectType, object! existingValue, Newtonsoft.Json.JsonSerializer! serializer) -> object?
-override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ValidationProblemDetailsConverter.WriteJson(Newtonsoft.Json.JsonWriter! writer, object! value, Newtonsoft.Json.JsonSerializer! serializer) -> void
+override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ValidationProblemDetailsConverter.ReadJson(Newtonsoft.Json.JsonReader! reader, System.Type! objectType, object? existingValue, Newtonsoft.Json.JsonSerializer! serializer) -> object?
+override Microsoft.AspNetCore.Mvc.NewtonsoftJson.ValidationProblemDetailsConverter.WriteJson(Newtonsoft.Json.JsonWriter! writer, object? value, Newtonsoft.Json.JsonSerializer! serializer) -> void
 ~static Microsoft.AspNetCore.Mvc.JsonPatchExtensions.ApplyTo<T>(this Microsoft.AspNetCore.JsonPatch.JsonPatchDocument<T!>! patchDoc, T! objectToApplyTo, Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary! modelState) -> void
 ~static Microsoft.AspNetCore.Mvc.JsonPatchExtensions.ApplyTo<T>(this Microsoft.AspNetCore.JsonPatch.JsonPatchDocument<T!>! patchDoc, T! objectToApplyTo, Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary! modelState, string! prefix) -> void
 static Microsoft.AspNetCore.Mvc.NewtonsoftJson.JsonSerializerSettingsProvider.CreateSerializerSettings() -> Newtonsoft.Json.JsonSerializerSettings!
diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/ValidationProblemDetailsConverter.cs b/src/Mvc/Mvc.NewtonsoftJson/src/ValidationProblemDetailsConverter.cs
index a7a0cdd1bea..5a87970f6ad 100644
--- a/src/Mvc/Mvc.NewtonsoftJson/src/ValidationProblemDetailsConverter.cs
+++ b/src/Mvc/Mvc.NewtonsoftJson/src/ValidationProblemDetailsConverter.cs
@@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Mvc.NewtonsoftJson
         }
 
         /// <inheritdoc />
-        public override object? ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+        public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
         {
             var annotatedProblemDetails = serializer.Deserialize<AnnotatedValidationProblemDetails>(reader);
             if (annotatedProblemDetails == null)
@@ -27,14 +27,14 @@ namespace Microsoft.AspNetCore.Mvc.NewtonsoftJson
                 return null;
             }
 
-            var problemDetails = (ValidationProblemDetails)existingValue ?? new ValidationProblemDetails();
+            var problemDetails = (ValidationProblemDetails?)existingValue ?? new ValidationProblemDetails();
             annotatedProblemDetails.CopyTo(problemDetails);
 
             return problemDetails;
         }
 
         /// <inheritdoc />
-        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+        public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
         {
             if (value == null)
             {
diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj
index b0d8b4c9877..0c11cef119d 100644
--- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj
+++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Microsoft.AspNetCore.Razor.Language.Test.csproj
@@ -17,7 +17,7 @@
 
   <ItemGroup>
     <Reference Include="Microsoft.AspNetCore.Razor.Language" />
-    <Reference Include="Newtonsoft.Json" Version="12.0.3" />
+    <Reference Include="Newtonsoft.Json" />
     <ProjectReference Include="..\..\test\Microsoft.AspNetCore.Razor.Test.Common\Microsoft.AspNetCore.Razor.Test.Common.csproj" />
     <ProjectReference Include="..\..\test\Microsoft.AspNetCore.Razor.Test.ComponentShim\Microsoft.AspNetCore.Razor.Test.ComponentShim.csproj" />
   </ItemGroup>
diff --git a/src/SignalR/common/Protocols.NewtonsoftJson/src/Protocol/NewtonsoftJsonHubProtocol.cs b/src/SignalR/common/Protocols.NewtonsoftJson/src/Protocol/NewtonsoftJsonHubProtocol.cs
index aea69001002..86c9e0a6c05 100644
--- a/src/SignalR/common/Protocols.NewtonsoftJson/src/Protocol/NewtonsoftJsonHubProtocol.cs
+++ b/src/SignalR/common/Protocols.NewtonsoftJson/src/Protocol/NewtonsoftJsonHubProtocol.cs
@@ -150,7 +150,7 @@ namespace Microsoft.AspNetCore.SignalR.Protocol
                         switch (reader.TokenType)
                         {
                             case JsonToken.PropertyName:
-                                var memberName = reader.Value.ToString();
+                                var memberName = reader.Value?.ToString();
 
                                 switch (memberName)
                                 {
@@ -430,7 +430,7 @@ namespace Microsoft.AspNetCore.SignalR.Protocol
                 switch (reader.TokenType)
                 {
                     case JsonToken.PropertyName:
-                        var propertyName = reader.Value.ToString()!;
+                        var propertyName = reader.Value!.ToString()!;
 
                         JsonUtils.CheckRead(reader);
 
diff --git a/src/SignalR/common/Shared/JsonUtils.cs b/src/SignalR/common/Shared/JsonUtils.cs
index 56d86d6be2a..d79b5a06595 100644
--- a/src/SignalR/common/Shared/JsonUtils.cs
+++ b/src/SignalR/common/Shared/JsonUtils.cs
@@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.Internal
             return (JObject)token;
         }
 
-        public static T GetRequiredProperty<T>(JObject json, string property, JTokenType expectedType = JTokenType.None)
+        public static T? GetRequiredProperty<T>(JObject json, string property, JTokenType expectedType = JTokenType.None)
         {
             var prop = json[property];
 
@@ -59,7 +59,7 @@ namespace Microsoft.AspNetCore.Internal
             return GetValue<T>(property, expectedType, prop);
         }
 
-        public static T GetValue<T>(string property, JTokenType expectedType, JToken prop)
+        public static T? GetValue<T>(string property, JTokenType expectedType, JToken prop)
         {
             if (expectedType != JTokenType.None && prop.Type != expectedType)
             {
@@ -192,8 +192,13 @@ namespace Microsoft.AspNetCore.Internal
                 return _inner.Rent(minimumLength);
             }
 
-            public void Return(T[] array)
+            public void Return(T[]? array)
             {
+                if (array is null)
+                {
+                    return;
+                }
+
                 _inner.Return(array);
             }
         }
-- 
GitLab