-
Notifications
You must be signed in to change notification settings - Fork 113
Migrate Spdx22ComponentDetector from Newtonsoft.Json to System.Text.json
#1571
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
base: users/jamagee/go-vcpkg-system.text.json
Are you sure you want to change the base?
Migrate Spdx22ComponentDetector from Newtonsoft.Json to System.Text.json
#1571
Conversation
….Json` - `VcpkgComponentDetector`: Replace `JsonConvert.DeserializeObject` with `JsonSerializer.Deserialize` - `GoCLIParser`: Replace `JsonTextReader`/`JsonSerializer` with `Utf8JsonReader` for parsing multi-object JSON output from `go list` - .NET 9 added `AllowMultipleValues`, but unfortunately we are still on .NET 8 - Add explicit `[JsonPropertyName]` attributes to vcpkg contract classes using SPDX 2.2.1 schema field names
…ext.Json` - Replace `JObject`/`JsonTextReader` with `JsonDocument.ParseAsync` - Convert `JObject` property access to `JsonElement.TryGetProperty` pattern - Make `OnFileFoundAsync` properly `async` - Update exception handling from `JsonReaderException` to `JsonException`
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## users/jamagee/go-vcpkg-system.text.json #1571 +/- ##
=======================================================================
Coverage 89.5% 89.6%
=======================================================================
Files 426 426
Lines 36268 36272 +4
Branches 2262 2262
=======================================================================
+ Hits 32494 32501 +7
+ Misses 3312 3309 -3
Partials 462 462 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR migrates the Spdx22ComponentDetector from Newtonsoft.Json to System.Text.Json, aligning with modern .NET standards. The detector parses SPDX 2.2 SBOM files in JSON format to extract component metadata.
Key changes:
- Replaced
JObject/JsonTextReaderwithJsonDocument.ParseAsyncfor async JSON parsing - Converted property access patterns from nullable
?.operators to explicitTryGetPropertychecks - Updated exception handling from
JsonReaderExceptiontoJsonException
Comments suppressed due to low confidence (1)
src/Microsoft.ComponentDetection.Detectors/spdx/Spdx22ComponentDetector.cs:1
- The
JsonDocumentreturned byParseAsyncshould be disposed. In DotNetComponentDetector (line 281), the document is not wrapped in ausingstatement, which could lead to a memory leak. However, in this file (line 60), you correctly useusing var document. Consider checking if DotNetComponentDetector needs the same fix, but this current usage is correct.
#nullable disable
| var sbomNamespace = document.TryGetProperty("documentNamespace", out var nsElement) ? nsElement.GetString() : null; | ||
| var name = document.TryGetProperty("name", out var nameElement) ? nameElement.GetString() : null; | ||
| var spdxVersion = document.TryGetProperty("spdxVersion", out var versionElement2) ? versionElement2.GetString() : null; |
Copilot
AI
Nov 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SpdxComponent constructor calls ValidateRequiredInput on all parameters (see SpdxComponent.cs:16-21), which will throw if these values are null. However, these properties may legitimately be missing from malformed SPDX documents. This will cause the component creation to throw an unhandled exception instead of gracefully logging a warning. Consider adding null checks before constructing the component, or catching the validation exception and logging an appropriate warning message.
| var spdxVersion = document["spdxVersion"]?.ToString(); | ||
| var sbomNamespace = document.TryGetProperty("documentNamespace", out var nsElement) ? nsElement.GetString() : null; | ||
| var name = document.TryGetProperty("name", out var nameElement) ? nameElement.GetString() : null; | ||
| var spdxVersion = document.TryGetProperty("spdxVersion", out var versionElement2) ? versionElement2.GetString() : null; |
Copilot
AI
Nov 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name versionElement2 is unclear and suggests a copy-paste error. It should be renamed to spdxVersionElement or versionElementSpdx to better indicate its purpose and distinguish it from versionElement used in IsSPDXVersionSupported.
| var spdxVersion = document.TryGetProperty("spdxVersion", out var versionElement2) ? versionElement2.GetString() : null; | |
| var spdxVersion = document.TryGetProperty("spdxVersion", out var spdxVersionElement) ? spdxVersionElement.GetString() : null; |
b7ebff5 to
bd4dcaa
Compare
JObject/JsonTextReaderwithJsonDocument.ParseAsyncJObjectproperty access toJsonElement.TryGetPropertypatternOnFileFoundAsyncproperlyasyncJsonReaderExceptiontoJsonException