From a6b19d6b39e19a6b81baac5acee726657e826a0b Mon Sep 17 00:00:00 2001 From: Yan Yagudayev Date: Thu, 25 Nov 2021 14:45:42 +0200 Subject: [PATCH] fixin bug in jsonsaver - avoid producing "Files: null" and "Snippets: null" if saveing a package without files (filesAnalyzed is false) Signed-off-by: Yan Yagudayev --- jsonsaver/saver2v2/save_document.go | 2 +- jsonsaver/saver2v2/save_document_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/jsonsaver/saver2v2/save_document.go b/jsonsaver/saver2v2/save_document.go index 43ea9072..21316775 100644 --- a/jsonsaver/saver2v2/save_document.go +++ b/jsonsaver/saver2v2/save_document.go @@ -71,7 +71,7 @@ func RenderDocument2_2(doc *spdx.Document2_2, buf *bytes.Buffer) error { } // save files and snippets from spdx to json - if allfiles != nil { + if len(allfiles) > 0 { _, err = renderFiles2_2(doc, jsondocument, allfiles) if err != nil { return err diff --git a/jsonsaver/saver2v2/save_document_test.go b/jsonsaver/saver2v2/save_document_test.go index 9651367a..595eaf8a 100644 --- a/jsonsaver/saver2v2/save_document_test.go +++ b/jsonsaver/saver2v2/save_document_test.go @@ -88,6 +88,11 @@ func TestRenderDocument2_2(t *testing.T) { RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "Package"}, Relationship: "CONTAINS", }, + { + RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"}, + RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "p1"}, + Relationship: "CONTAINS", + }, { RefA: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"}, RefB: spdx.DocElementID{DocumentRefID: "", ElementRefID: "File"}, @@ -255,6 +260,11 @@ func TestRenderDocument2_2(t *testing.T) { "relatedSpdxElement": "SPDXRef-Package", "relationshipType": "CONTAINS", }, + map[string]interface{}{ + "spdxElementId": "SPDXRef-DOCUMENT", + "relatedSpdxElement": "SPDXRef-p1", + "relationshipType": "CONTAINS", + }, map[string]interface{}{ "spdxElementId": "SPDXRef-DOCUMENT", "relatedSpdxElement": "SPDXRef-File",