Skip to content

Commit 636f9a5

Browse files
ruromeroclaude
andcommitted
fix: match JS client empty SBOM structure in CycloneDX output
When all dependencies are filtered out, the Java client now produces the same SBOM structure as the JS client: "components": [] is always present and "dependencies": [] is empty (no orphan root dep entry). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4aa48c4 commit 636f9a5

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/main/java/io/github/guacsec/trustifyda/sbom/CycloneDXSbom.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ private Sbom removeIgnoredDepsFromSbom(List<String> refsToIgnore) {
230230
d.setDependencies(filteredDeps);
231231
}
232232
});
233+
if (bom.getComponents().isEmpty()) {
234+
bom.setDependencies(new ArrayList<>());
235+
}
233236
return this;
234237
}
235238

@@ -302,12 +305,26 @@ public Sbom addDependency(PackageURL sourceRef, PackageURL targetRef, String s)
302305
public String getAsJsonString() {
303306
try {
304307
var jsonString = BomGeneratorFactory.createJson(VERSION, bom).toJsonString();
308+
jsonString = ensureComponentsField(jsonString);
305309
if (debugLoggingIsNeeded()) {
306310
log.info("Generated Sbom Json:" + System.lineSeparator() + jsonString);
307311
}
308312
return jsonString;
309313
} catch (GeneratorException e) {
310-
throw new RuntimeException("Unable to genenerate JSON from SBOM", e);
314+
throw new RuntimeException("Unable to generate JSON from SBOM", e);
315+
}
316+
}
317+
318+
private String ensureComponentsField(String json) {
319+
try {
320+
var mapper = new com.fasterxml.jackson.databind.ObjectMapper();
321+
var root = (com.fasterxml.jackson.databind.node.ObjectNode) mapper.readTree(json);
322+
if (!root.has("components")) {
323+
root.set("components", mapper.createArrayNode());
324+
}
325+
return mapper.writeValueAsString(root);
326+
} catch (Exception e) {
327+
return json;
311328
}
312329
}
313330

src/test/resources/tst_manifests/golang/go_mod_with_all_ignore/expected_sbom_component_analysis.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
"purl": "pkg:golang/github.com/devfile-samples/devfile-sample-go-basic@v0.0.0"
1313
}
1414
},
15-
"dependencies": [
16-
{
17-
"ref": "pkg:golang/github.com/devfile-samples/devfile-sample-go-basic@v0.0.0",
18-
"dependsOn": [ ]
19-
}
20-
]
15+
"components": [ ],
16+
"dependencies": [ ]
2117
}

0 commit comments

Comments
 (0)