Replace Newtonsoft.Json with System.Text.Json in tests and samples#4323
Replace Newtonsoft.Json with System.Text.Json in tests and samples#4323paulmedynski wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Removes the Newtonsoft.Json dependency from all test and sample projects in favor of System.Text.Json, deletes a long-commented legacy AKV sample, and drops the central Newtonsoft.Json PackageVersion. The PR also bundles a number of unrelated pipeline, flaky-test, and unit-test reliability changes that are not described in the PR text.
Changes:
- Migrate
JsonBulkCopyTest,JsonStreamTest, andSqlExceptionTestto STJ; add sharedJsonTestHelperwithJsonNode.DeepEqualson .NET 9+ and a recursiveJsonElementfallback (depth-limited) on earlier TFMs. - Remove
Newtonsoft.JsonPackageReferences from ManualTests/FunctionalTests/Samples csprojs, remove its centralPackageVersion, and deleteAzureKeyVaultProviderLegacyExample_2_0.cs. - Unrelated extras: reset cached switches in
LocalAppContextSwitchesTest, mark several simulated-server testsflaky, gateAsyncCancelledConnectionsTestagainst Kerberos/Managed Instance, addretryCountOnTaskFailureto dotnet install tasks, switch OneBranch symbols variable group/casing, addrestore-dotnet-tools.ymlto OneBranch and Kerberos pipelines, and change Kerberos code-coverage stage condition.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| Directory.Packages.props | Drop central Newtonsoft.Json PackageVersion. |
| doc/samples/Microsoft.Data.SqlClient.Samples.csproj | Remove Newtonsoft.Json PackageReference. |
| doc/samples/AzureKeyVaultProviderLegacyExample_2_0.cs | Delete already-commented legacy AKV sample. |
| src/.../tests/FunctionalTests/Microsoft.Data.SqlClient.FunctionalTests.csproj | Remove Newtonsoft.Json PackageReference for net8/net462. |
| src/.../tests/FunctionalTests/SqlExceptionTest.cs | Replace Newtonsoft round-trip with STJ property-projection serialization; remove JSONSerializationTest. |
| src/.../tests/ManualTests/Microsoft.Data.SqlClient.ManualTests.csproj | Add JsonTestHelper.cs; drop Newtonsoft.Json PackageReferences. |
| src/.../tests/ManualTests/SQL/JsonTest/JsonBulkCopyTest.cs | Switch JSON serialization and file comparison to STJ + JsonTestHelper. |
| src/.../tests/ManualTests/SQL/JsonTest/JsonStreamTest.cs | Same STJ migration for the stream test. |
| src/.../tests/ManualTests/SQL/JsonTest/JsonTestHelper.cs | New depth-limited deep-equals helper (delegates to JsonNode.DeepEquals on .NET 9+). |
| src/.../tests/ManualTests/DataCommon/DataTestUtility.cs | Add IsNotKerberosTest helper used by gated tests. |
| src/.../tests/ManualTests/SQL/AsyncTest/AsyncCancelledConnectionsTest.cs | Gate CancelAsyncConnections against MI and Kerberos environments. |
| src/.../tests/UnitTests/Microsoft/Data/SqlClient/LocalAppContextSwitchesTest.cs | Reset cached switch fields via helper before asserting defaults. |
| src/.../tests/UnitTests/SimulatedServerTests/ConnectionFailoverTests.cs | Mark two transient-fault retry-disabled tests flaky. |
| src/.../tests/UnitTests/SimulatedServerTests/ConnectionRoutingTestsAzure.cs | Mark routed-location timeout test flaky. |
| eng/pipelines/steps/install-dotnet.yml | Add retryCountOnTaskFailure: 3 to dotnet install tasks. |
| eng/pipelines/onebranch/variables/onebranch-variables.yml | Switch symbols group to symbols-variables-v3 with Ppe casing. |
| eng/pipelines/onebranch/sqlclient-non-official.yml | Use new SymbolsPublishServerPpe / SymbolsPublishTokenUriPpe. |
| eng/pipelines/onebranch/jobs/build-buildproj-job.yml | Restore dotnet local tools before build. |
| eng/pipelines/kerberos/sqlclient-kerberos.yml | Restore dotnet local tools; tighten coverage stage condition to succeeded(). |
- Migrate JsonBulkCopyTest and JsonStreamTest to System.Text.Json - Add JsonTestHelper with DeepEquals supporting net8.0+ and net9.0+ - Rewrite SqlExceptionTest to serialize key properties via STJ - Remove JSONSerializationTest (tested Newtonsoft-specific ISerializable) - Delete legacy AzureKeyVaultProviderLegacyExample_2_0.cs (marked TODO) - Remove Newtonsoft.Json from all csproj files and Directory.Packages.props Tracks: #4322
4c5e6d7 to
b842414
Compare
There was a problem hiding this comment.
Issue #4322 tracks the corresponding MSDocs removal of references to this file's snippets.
| Assert.Equal(e.ClientConnectionId, sqlEx.ClientConnectionId); | ||
| Assert.Equal(e.StackTrace, sqlEx.StackTrace); | ||
| } | ||
| // Serialize the properties we want to validate round-trip through JSON. |
There was a problem hiding this comment.
The old test was confirming that Newtonsoft round-tripping functioned, which isn't our responsibility. Now we're checking that specific fields from our SqlException can be serialized, which is still not really our responsibility, but is at least more targeted.
| }); | ||
|
|
||
| [Fact] | ||
| public void JSONSerializationTest() |
There was a problem hiding this comment.
This test was actually a Newtonsoft unit test, which isn't something we should be testing.
System.Text.Json serializes supplementary-plane characters (e.g. U+29E3D) as \uD867\uDE3D escape sequences, while SQL Server returns them as literal UTF-8. The DeepEqualsCore fallback compared GetRawText() which preserves escaping differences. Add explicit JsonValueKind.String case using GetString() which decodes both representations to the same .NET string.
Description
Removes the Newtonsoft.Json dependency from all test and sample projects, replacing usage with System.Text.Json.
Based on #4297 which already migrated
TestUtilities/Config.cs.Changes
System.Text.Json+ newJsonTestHelperJsonNode.DeepEqualson .NET 9+ and a recursiveJsonElementcomparison on .NET 8/462 (with max-depth guard)JSONSerializationTest(tested Newtonsoft-specificISerializabledeserialization)Newtonsoft.Jsonfrom ManualTests, FunctionalTests, Samples csprojsNewtonsoft.JsonPackageVersion entryFollow-up
Testing
SqlExceptionTestpasses on net8.0, net9.0, net10.0*.cs,*.csproj,*.props