From 49dd80d40e7f0ddde43a0089eb31a558165a9489 Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Mon, 24 Nov 2025 11:22:56 -0800 Subject: [PATCH] Migrate `ComponentDetectionIntegrationTests` exclusively to `System.Text.Json` In #1523 we added the `[JsonProperty(Order = int.MinValue]` `Newtonsoft.Json` attribute to the `Type` property of the `TypedComponent` class to ensure that `"type"` would appear first in the serialized object. This is because `System.Text.Json` requires the type discriminator to be the first property of the object[^1]. `System.Text.Json` `9.0.0` and later does support the `JsonSerializerOptions.AllowOutOfOrder` option[^2], but as we're targeting .NET 8, we don't have access to that. [^1]: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism#polymorphic-type-discriminators [^2]: https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.allowoutofordermetadataproperties?view=net-10.0#system-text-json-jsonserializeroptions-allowoutofordermetadataproperties --- .../ComponentDetectionIntegrationTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Microsoft.ComponentDetection.VerificationTests/ComponentDetectionIntegrationTests.cs b/test/Microsoft.ComponentDetection.VerificationTests/ComponentDetectionIntegrationTests.cs index e60fa7060..a3e3b1fe3 100644 --- a/test/Microsoft.ComponentDetection.VerificationTests/ComponentDetectionIntegrationTests.cs +++ b/test/Microsoft.ComponentDetection.VerificationTests/ComponentDetectionIntegrationTests.cs @@ -5,12 +5,12 @@ namespace Microsoft.ComponentDetection.VerificationTests; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.Json; using System.Text.RegularExpressions; using AwesomeAssertions; using AwesomeAssertions.Execution; using Microsoft.ComponentDetection.Contracts.BcdeModels; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json; [TestClass] public class ComponentDetectionIntegrationTests @@ -283,11 +283,11 @@ private void SetupGithub(string oldGithubArtifactsDir, string newGithubArtifacts { var oldGithubDirectory = new DirectoryInfo(oldGithubArtifactsDir); this.oldLogFileContents = this.GetFileTextWithPattern("GovCompDisc_Log*.log", oldGithubDirectory); - this.oldScanResult = JsonConvert.DeserializeObject(this.GetFileTextWithPattern("ScanManifest*.json", oldGithubDirectory)); + this.oldScanResult = JsonSerializer.Deserialize(this.GetFileTextWithPattern("ScanManifest*.json", oldGithubDirectory)); var newGithubDirectory = new DirectoryInfo(newGithubArtifactsDir); this.newLogFileContents = this.GetFileTextWithPattern("GovCompDisc_Log*.log", newGithubDirectory); - this.newScanResult = System.Text.Json.JsonSerializer.Deserialize(this.GetFileTextWithPattern("ScanManifest*.json", newGithubDirectory)); + this.newScanResult = JsonSerializer.Deserialize(this.GetFileTextWithPattern("ScanManifest*.json", newGithubDirectory)); } private string GetFileTextWithPattern(string pattern, DirectoryInfo directory)