From 80032f1b16d3b6c819d64ebfd81f0f8885f94fb4 Mon Sep 17 00:00:00 2001 From: Brandon Lum Date: Mon, 5 Jun 2023 20:40:10 -0400 Subject: [PATCH] fix duplicate shorthand relationships for opposite case Signed-off-by: Brandon Lum --- spdx/v2/v2_2/document.go | 18 +++++++++++++++++- spdx/v2/v2_2/json/json_test.go | 12 ++++++------ spdx/v2/v2_3/document.go | 18 +++++++++++++++++- spdx/v2/v2_3/json/json_test.go | 12 ++++++------ 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/spdx/v2/v2_2/document.go b/spdx/v2/v2_2/document.go index d94f5b0..c7dae44 100644 --- a/spdx/v2/v2_2/document.go +++ b/spdx/v2/v2_2/document.go @@ -101,7 +101,23 @@ func (d *Document) UnmarshalJSON(b []byte) error { relationshipExists := map[string]bool{} serializeRel := func(r *Relationship) string { - return fmt.Sprintf("%v-%v->%v", common.RenderDocElementID(r.RefA), r.Relationship, common.RenderDocElementID(r.RefB)) + refA := r.RefA + refB := r.RefB + rel := r.Relationship + + // we need to serialize the opposite for CONTAINED_BY and DESCRIBED_BY + // so that it will match when we try to de-duplicate during deserialization. + switch r.Relationship { + case common.TypeRelationshipContainedBy: + rel = common.TypeRelationshipContains + refA = r.RefB + refB = r.RefA + case common.TypeRelationshipDescribeBy: + rel = common.TypeRelationshipDescribe + refA = r.RefB + refB = r.RefA + } + return fmt.Sprintf("%v-%v->%v", common.RenderDocElementID(refA), rel, common.RenderDocElementID(refB)) } // index current list of relationships to ensure no duplication diff --git a/spdx/v2/v2_2/json/json_test.go b/spdx/v2/v2_2/json/json_test.go index a4a0275..c84c0ad 100644 --- a/spdx/v2/v2_2/json/json_test.go +++ b/spdx/v2/v2_2/json/json_test.go @@ -227,9 +227,9 @@ func Test_ShorthandFieldsNoDuplicates(t *testing.T) { "relatedSpdxElement": "SPDXRef-File-1" }, { - "spdxElementId": "SPDXRef-Package-1", - "relationshipType": "CONTAINS", - "relatedSpdxElement": "SPDXRef-File-2" + "spdxElementId": "SPDXRef-File-2", + "relationshipType": "CONTAINED_BY", + "relatedSpdxElement": "SPDXRef-Package-1" } ] }` @@ -291,9 +291,9 @@ func Test_ShorthandFieldsNoDuplicates(t *testing.T) { Relationship: common.TypeRelationshipContains, }, { - RefA: id("Package-1"), - RefB: id("File-2"), - Relationship: common.TypeRelationshipContains, + RefA: id("File-2"), + RefB: id("Package-1"), + Relationship: common.TypeRelationshipContainedBy, }, }, } diff --git a/spdx/v2/v2_3/document.go b/spdx/v2/v2_3/document.go index 279e976..7350192 100644 --- a/spdx/v2/v2_3/document.go +++ b/spdx/v2/v2_3/document.go @@ -100,7 +100,23 @@ func (d *Document) UnmarshalJSON(b []byte) error { relationshipExists := map[string]bool{} serializeRel := func(r *Relationship) string { - return fmt.Sprintf("%v-%v->%v", common.RenderDocElementID(r.RefA), r.Relationship, common.RenderDocElementID(r.RefB)) + refA := r.RefA + refB := r.RefB + rel := r.Relationship + + // we need to serialize the opposite for CONTAINED_BY and DESCRIBED_BY + // so that it will match when we try to de-duplicate during deserialization. + switch r.Relationship { + case common.TypeRelationshipContainedBy: + rel = common.TypeRelationshipContains + refA = r.RefB + refB = r.RefA + case common.TypeRelationshipDescribeBy: + rel = common.TypeRelationshipDescribe + refA = r.RefB + refB = r.RefA + } + return fmt.Sprintf("%v-%v->%v", common.RenderDocElementID(refA), rel, common.RenderDocElementID(refB)) } // index current list of relationships to ensure no duplication diff --git a/spdx/v2/v2_3/json/json_test.go b/spdx/v2/v2_3/json/json_test.go index ea648df..b7b004a 100644 --- a/spdx/v2/v2_3/json/json_test.go +++ b/spdx/v2/v2_3/json/json_test.go @@ -245,9 +245,9 @@ func Test_ShorthandFieldsNoDuplicates(t *testing.T) { "relatedSpdxElement": "SPDXRef-File-1" }, { - "spdxElementId": "SPDXRef-Package-1", - "relationshipType": "CONTAINS", - "relatedSpdxElement": "SPDXRef-File-2" + "spdxElementId": "SPDXRef-File-2", + "relationshipType": "CONTAINED_BY", + "relatedSpdxElement": "SPDXRef-Package-1" } ] }` @@ -309,9 +309,9 @@ func Test_ShorthandFieldsNoDuplicates(t *testing.T) { Relationship: common.TypeRelationshipContains, }, { - RefA: id("Package-1"), - RefB: id("File-2"), - Relationship: common.TypeRelationshipContains, + RefA: id("File-2"), + RefB: id("Package-1"), + Relationship: common.TypeRelationshipContainedBy, }, }, }