Fix package json deserialization 'engines' property#1629
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1629 +/- ##
=======================================
+ Coverage 90.3% 90.4% +0.1%
=======================================
Files 440 441 +1
Lines 38130 38314 +184
Branches 2347 2347
=======================================
+ Hits 34465 34673 +208
+ Misses 3183 3159 -24
Partials 482 482 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a deserialization issue for the engines property in npm package-lock.json files by applying the existing PackageJsonEnginesConverter to PackageLockV2Package and PackageLockV3Package classes. The converter handles both the modern object format and legacy array format for the engines field, preventing deserialization errors when encountering old npm packages.
Changes:
- Added
[JsonConverter(typeof(PackageJsonEnginesConverter))]attribute to theEnginesproperty in bothPackageLockV2PackageandPackageLockV3Packageclasses - Created comprehensive test suite covering object format, array format (including vscode detection), null values, missing fields, and serialization round-trips for both V2 and V3 package classes
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Microsoft.ComponentDetection.Detectors/npm/Contracts/PackageLockV2Package.cs | Added JsonConverter attribute to Engines property to handle array and object formats |
| src/Microsoft.ComponentDetection.Detectors/npm/Contracts/PackageLockV3Package.cs | Added JsonConverter attribute to Engines property to handle array and object formats |
| test/Microsoft.ComponentDetection.Detectors.Tests/npm/Contracts/PackageLockPackageEnginesTests.cs | Added comprehensive test coverage for engines property deserialization in both V2 and V3 package formats |
|
👋 Hi! It looks like you modified some files in the
If none of the above scenarios apply, feel free to ignore this comment 🙂 |
Context
Recently
System.Text.Jsonserialization library was introduced, this required significant changes in the repository to ensure backwards compatibility with Newtonsoft behaviors.Issue
As I was validating unrelated work, I found that certain NPM package-lock json files can error out if the "engines" property is declared as an array instead of a dictionary.
enginesfield historically had more flexibility. In very old npm/package.json specs (pre-npm 1.0), engines could be specified as:The array format was deprecated long ago, but some very old packages still have it in their published metadata, and npm preserves this when generating package-lock.json files. See sample package with an array https://registry.npmjs.org/concat-stream/1.4.6, when official doc currently expects an object see NPM engines
Error log
Solution
Use the serialization utility that was introduced for other classes, see
PackageJsonEnginesConverterutility in #1572