fix duplicate shorthand relationships for opposite case
Signed-off-by: Brandon Lum <lumjjb@gmail.com>
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 @@
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 @@
"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 @@
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 @@
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 @@
"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 @@
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,
},
},
}