fix: Omit null fields in CycloneDX SBOM license export
What does this MR do and why?
-
The dependency list exports feature fails with
SchemaValidationErrorwhen exporting CycloneDX SBOMs that have missing optional fields. Although the ingested SBOMs pass CycloneDX validation, GitLab's export fails because null values for optional fields (like license IDs or component versions) are included in the JSON output, violating the CycloneDX schema. -
This MR adds
expose_nil: falseto the optional fields in the SBOM serialization code:- License properties (
spdx_identifier,name,url) - Component properties (
version,purl,licenses)
- License properties (
-
In addition, the code now properly validates licenses with the following rules:
- Valid SPDX ID is the primary validator (even with unknown name) - the license exposure logic was refactored to prioritize SPDX identifiers over names, replacing the previous
unknown?method with avalid_id?method that handles all license validation scenarios. - license fields with
nullvalues are omitted from the output JSON. - components with
nulllicenses or empty license arrays skip license serialization.
- Valid SPDX ID is the primary validator (even with unknown name) - the license exposure logic was refactored to prioritize SPDX identifiers over names, replacing the previous
The following scenarios are now properly handled:
| Case | SPDX Identifier | Name | Expected |
|---|---|---|---|
| Standard | MIT |
MIT |
{ id: "MIT", url: "https://spdx.org/licenses/MIT.html" } |
| Valid ID, custom name | Apache-2.0 |
Some License |
{ id: "Apache-2.0", url: "https://spdx.org/licenses/Apache-2.0.html" } |
| Unknown ID and name | unknown |
unknown |
{ name: "unknown" } |
| Invalid ID, custom name |
nil / "" / unknown
|
Some License |
{ name: "Some License" } |
| Valid ID, invalid name | Apache-2.0 |
nil / "" / unknown
|
{ id: "Apache-2.0", url: "https://spdx.org/licenses/Apache-2.0.html" } |
Related issues
- The issue: CycloneDX SBOM export fails validation when pro... (#517492 - closed) • Orin Naaman • 17.11 • At risk
- RFH (internal): https://gitlab.com/gitlab-com/request-for-help/-/issues/2292+s
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Screenshots or screen recordings
Screenshots are required for UI changes, and strongly recommended for all other merge requests.
| Before | After |
|---|---|
How to set up and validate locally
Numbered steps to set up and validate the change are strongly suggested.