From 82e041c27c1c20cd3700aefce3fe1bdb35cb65ba Mon Sep 17 00:00:00 2001 From: liangli Date: Fri, 29 Jul 2022 10:12:28 +0800 Subject: [PATCH] fix: replace all newtonsoft.json usages (#3478) --- build/Common.nonprod.props | 2 +- .../BasicTests.cs | 10 ++-- .../HttpClientTests.netcore31.cs | 49 ++++++++++--------- .../HttpTestData.cs | 8 ++- .../HttpWebRequestTests.netfx.cs | 43 ++++++++-------- ...elemetry.Instrumentation.Http.Tests.csproj | 2 +- .../http-out-test-cases.json | 16 +++--- .../Controllers/ForwardController.cs | 9 ++-- .../Controllers/ForwardController.cs | 9 ++-- 9 files changed, 75 insertions(+), 73 deletions(-) diff --git a/build/Common.nonprod.props b/build/Common.nonprod.props index 704d4f890fb..4810547d72e 100644 --- a/build/Common.nonprod.props +++ b/build/Common.nonprod.props @@ -41,11 +41,11 @@ [6.0.0,) [6.0.0,) [16.10.0] - [13.0.1,14.0) [4.14.5,5.0) [6.1.0,7.0) [1.0.0-rc.2,2.0) [6.2.3] + 6.0.5 [2.4.3,3.0) [2.4.1,3.0) diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs index 208b12da777..f7eaf9bd213 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs @@ -19,6 +19,7 @@ using System.Diagnostics; using System.Linq; using System.Net.Http; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -26,7 +27,6 @@ using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Moq; -using Newtonsoft.Json; using OpenTelemetry.Context.Propagation; using OpenTelemetry.Instrumentation.AspNetCore.Implementation; using OpenTelemetry.Tests; @@ -364,7 +364,7 @@ public async Task ExtractContextIrrespectiveOfSamplingDecision(SamplingDecision // Test TraceContext Propagation var request = new HttpRequestMessage(HttpMethod.Get, "/api/GetChildActivityTraceContext"); var response = await client.SendAsync(request); - var childActivityTraceContext = JsonConvert.DeserializeObject>(response.Content.ReadAsStringAsync().Result); + var childActivityTraceContext = JsonSerializer.Deserialize>(response.Content.ReadAsStringAsync().Result); response.EnsureSuccessStatusCode(); @@ -376,7 +376,7 @@ public async Task ExtractContextIrrespectiveOfSamplingDecision(SamplingDecision request = new HttpRequestMessage(HttpMethod.Get, "/api/GetChildActivityBaggageContext"); response = await client.SendAsync(request); - var childActivityBaggageContext = JsonConvert.DeserializeObject>(response.Content.ReadAsStringAsync().Result); + var childActivityBaggageContext = JsonSerializer.Deserialize>(response.Content.ReadAsStringAsync().Result); response.EnsureSuccessStatusCode(); @@ -431,7 +431,7 @@ public async Task ExtractContextIrrespectiveOfTheFilterApplied() // Ensure that filter was called Assert.True(isFilterCalled); - var childActivityTraceContext = JsonConvert.DeserializeObject>(response.Content.ReadAsStringAsync().Result); + var childActivityTraceContext = JsonSerializer.Deserialize>(response.Content.ReadAsStringAsync().Result); response.EnsureSuccessStatusCode(); @@ -443,7 +443,7 @@ public async Task ExtractContextIrrespectiveOfTheFilterApplied() request = new HttpRequestMessage(HttpMethod.Get, "/api/GetChildActivityBaggageContext"); response = await client.SendAsync(request); - var childActivityBaggageContext = JsonConvert.DeserializeObject>(response.Content.ReadAsStringAsync().Result); + var childActivityBaggageContext = JsonSerializer.Deserialize>(response.Content.ReadAsStringAsync().Result); response.EnsureSuccessStatusCode(); diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.netcore31.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.netcore31.cs index 27db9e89fba..05c39935426 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.netcore31.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.netcore31.cs @@ -22,9 +22,9 @@ using System.Linq; using System.Net.Http; using System.Reflection; +using System.Text.Json; using System.Threading.Tasks; using Moq; -using Newtonsoft.Json; using OpenTelemetry.Metrics; using OpenTelemetry.Tests; using OpenTelemetry.Trace; @@ -185,29 +185,30 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOut [Fact] public async Task DebugIndividualTestAsync() { - var serializer = new JsonSerializer(); - var input = serializer.Deserialize(new JsonTextReader(new StringReader(@" -[ - { - ""name"": ""Response code: 399"", - ""method"": ""GET"", - ""url"": ""http://{host}:{port}/"", - ""responseCode"": 399, - ""responseExpected"": true, - ""spanName"": ""HTTP GET"", - ""spanStatus"": ""UNSET"", - ""spanKind"": ""Client"", - ""spanAttributes"": { - ""http.scheme"": ""http"", - ""http.method"": ""GET"", - ""http.host"": ""{host}:{port}"", - ""http.status_code"": ""399"", - ""http.flavor"": ""2.0"", - ""http.url"": ""http://{host}:{port}/"" - } - } -] -"))); + var input = JsonSerializer.Deserialize( + @" + [ + { + ""name"": ""Response code: 399"", + ""method"": ""GET"", + ""url"": ""http://{host}:{port}/"", + ""responseCode"": 399, + ""responseExpected"": true, + ""spanName"": ""HTTP GET"", + ""spanStatus"": ""UNSET"", + ""spanKind"": ""Client"", + ""spanAttributes"": { + ""http.scheme"": ""http"", + ""http.method"": ""GET"", + ""http.host"": ""{host}:{port}"", + ""http.status_code"": ""399"", + ""http.flavor"": ""2.0"", + ""http.url"": ""http://{host}:{port}/"" + } + } + ] + ", + new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); var t = (Task)this.GetType().InvokeMember(nameof(this.HttpOutCallsAreCollectedSuccessfullyAsync), BindingFlags.InvokeMethod, null, this, HttpTestData.GetArgumentsFromTestCaseObject(input).First()); await t; diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpTestData.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpTestData.cs index 38e1dd83391..aa8efe91fbe 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpTestData.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpTestData.cs @@ -14,9 +14,8 @@ // limitations under the License. // using System.Collections.Generic; -using System.IO; using System.Reflection; -using Newtonsoft.Json; +using System.Text.Json; namespace OpenTelemetry.Instrumentation.Http.Tests { @@ -25,9 +24,8 @@ public static class HttpTestData public static IEnumerable ReadTestCases() { var assembly = Assembly.GetExecutingAssembly(); - var serializer = new JsonSerializer(); - var input = serializer.Deserialize(new JsonTextReader(new StreamReader(assembly.GetManifestResourceStream("OpenTelemetry.Instrumentation.Http.Tests.http-out-test-cases.json")))); - + var input = JsonSerializer.Deserialize( + assembly.GetManifestResourceStream("OpenTelemetry.Instrumentation.Http.Tests.http-out-test-cases.json"), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); return GetArgumentsFromTestCaseObject(input); } diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs index 3d3e10a0400..a31ddbaaf43 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpWebRequestTests.netfx.cs @@ -20,8 +20,8 @@ using System.IO; using System.Linq; using System.Net; +using System.Text.Json; using Moq; -using Newtonsoft.Json; using OpenTelemetry.Tests; using OpenTelemetry.Trace; using Xunit; @@ -137,26 +137,27 @@ public void HttpOutCallsAreCollectedSuccessfully(HttpTestData.HttpOutTestCase tc [Fact] public void DebugIndividualTest() { - var serializer = new JsonSerializer(); - var input = serializer.Deserialize(new JsonTextReader(new StringReader(@" - { - ""name"": ""Http version attribute populated"", - ""method"": ""GET"", - ""url"": ""http://{host}:{port}/"", - ""responseCode"": 200, - ""spanName"": ""HTTP GET"", - ""spanStatus"": ""UNSET"", - ""spanKind"": ""Client"", - ""setHttpFlavor"": true, - ""spanAttributes"": { - ""http.method"": ""GET"", - ""http.host"": ""{host}:{port}"", - ""http.flavor"": ""2.0"", - ""http.status_code"": 200, - ""http.url"": ""http://{host}:{port}/"" - } - } -"))); + var input = JsonSerializer.Deserialize( + @" + { + ""name"": ""Http version attribute populated"", + ""method"": ""GET"", + ""url"": ""http://{host}:{port}/"", + ""responseCode"": 200, + ""spanName"": ""HTTP GET"", + ""spanStatus"": ""UNSET"", + ""spanKind"": ""Client"", + ""setHttpFlavor"": true, + ""spanAttributes"": { + ""http.method"": ""GET"", + ""http.host"": ""{host}:{port}"", + ""http.flavor"": ""2.0"", + ""http.status_code"": ""200"", + ""http.url"": ""http://{host}:{port}/"" + } + } + ", + new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); this.HttpOutCallsAreCollectedSuccessfully(input); } diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/OpenTelemetry.Instrumentation.Http.Tests.csproj b/test/OpenTelemetry.Instrumentation.Http.Tests/OpenTelemetry.Instrumentation.Http.Tests.csproj index a38f1a99cba..35f23f1ba4b 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/OpenTelemetry.Instrumentation.Http.Tests.csproj +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/OpenTelemetry.Instrumentation.Http.Tests.csproj @@ -15,9 +15,9 @@ - + all diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/http-out-test-cases.json b/test/OpenTelemetry.Instrumentation.Http.Tests/http-out-test-cases.json index aeae7bf8ade..3441e152d92 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/http-out-test-cases.json +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/http-out-test-cases.json @@ -11,7 +11,7 @@ "http.method": "GET", "http.host": "{host}:{port}", "http.flavor": "2.0", - "http.status_code": 200, + "http.status_code": "200", "http.url": "http://{host}:{port}/" } }, @@ -27,7 +27,7 @@ "http.method": "POST", "http.host": "{host}:{port}", "http.flavor": "2.0", - "http.status_code": 200, + "http.status_code": "200", "http.url": "http://{host}:{port}/" } }, @@ -44,7 +44,7 @@ "http.method": "GET", "http.host": "{host}:{port}", "http.flavor": "2.0", - "http.status_code": 200, + "http.status_code": "200", "http.url": "http://{host}:{port}/path/to/resource/" } }, @@ -61,7 +61,7 @@ "http.method": "GET", "http.host": "{host}:{port}", "http.flavor": "2.0", - "http.status_code": 200, + "http.status_code": "200", "http.url": "http://{host}:{port}/path/to/resource#fragment" } }, @@ -78,7 +78,7 @@ "http.method": "GET", "http.host": "{host}:{port}", "http.flavor": "2.0", - "http.status_code": 200, + "http.status_code": "200", "http.url": "http://{host}:{port}/path/to/resource#fragment" } }, @@ -129,7 +129,7 @@ "http.method": "GET", "http.host": "{host}:{port}", "http.flavor": "2.0", - "http.status_code": 200, + "http.status_code": "200", "http.url": "http://{host}:{port}/" } }, @@ -146,7 +146,7 @@ "http.method": "GET", "http.host": "{host}:{port}", "http.flavor": "2.0", - "http.status_code": 200, + "http.status_code": "200", "http.url": "http://{host}:{port}/" } }, @@ -333,7 +333,7 @@ "http.method": "GET", "http.host": "{host}:{port}", "http.flavor": "2.0", - "http.status_code": 200, + "http.status_code": "200", "http.url": "http://{host}:{port}/" } } diff --git a/test/TestApp.AspNetCore.3.1/Controllers/ForwardController.cs b/test/TestApp.AspNetCore.3.1/Controllers/ForwardController.cs index 31b98f6955c..5b532d3af0e 100644 --- a/test/TestApp.AspNetCore.3.1/Controllers/ForwardController.cs +++ b/test/TestApp.AspNetCore.3.1/Controllers/ForwardController.cs @@ -15,9 +15,10 @@ // using System.Net.Http; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json; namespace TestApp.AspNetCore._3._1.Controllers { @@ -44,7 +45,7 @@ public async Task Post([FromBody] Data[] data) var request = new HttpRequestMessage(HttpMethod.Post, argument.Url) { Content = new StringContent( - JsonConvert.SerializeObject(argument.Arguments), + JsonSerializer.Serialize(argument.Arguments), Encoding.UTF8, "application/json"), }; @@ -61,10 +62,10 @@ public async Task Post([FromBody] Data[] data) public class Data { - [JsonProperty("url")] + [JsonPropertyName("url")] public string Url { get; set; } - [JsonProperty("arguments")] + [JsonPropertyName("arguments")] public Data[] Arguments { get; set; } } } diff --git a/test/TestApp.AspNetCore.6.0/Controllers/ForwardController.cs b/test/TestApp.AspNetCore.6.0/Controllers/ForwardController.cs index 36290e144c9..3f1a867186e 100644 --- a/test/TestApp.AspNetCore.6.0/Controllers/ForwardController.cs +++ b/test/TestApp.AspNetCore.6.0/Controllers/ForwardController.cs @@ -16,9 +16,10 @@ using System.Net.Http; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json; namespace TestApp.AspNetCore._6._0.Controllers { @@ -45,7 +46,7 @@ public async Task Post([FromBody] Data[] data) var request = new HttpRequestMessage(HttpMethod.Post, argument.Url) { Content = new StringContent( - JsonConvert.SerializeObject(argument.Arguments), + JsonSerializer.Serialize(argument.Arguments), Encoding.UTF8, "application/json"), }; @@ -62,10 +63,10 @@ public async Task Post([FromBody] Data[] data) public class Data { - [JsonProperty("url")] + [JsonPropertyName("url")] public string Url { get; set; } - [JsonProperty("arguments")] + [JsonPropertyName("arguments")] public Data[] Arguments { get; set; } } }