Skip to content

Commit

Permalink
fix duplicate shorthand relationships for opposite case
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Lum <lumjjb@gmail.com>
  • Loading branch information
lumjjb committed Jun 6, 2023
1 parent e939624 commit 80032f1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
18 changes: 17 additions & 1 deletion spdx/v2/v2_2/document.go
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spdx/v2/v2_2/json/json_test.go
Expand Up @@ -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"
}
]
}`
Expand Down Expand Up @@ -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,
},
},
}
Expand Down
18 changes: 17 additions & 1 deletion spdx/v2/v2_3/document.go
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spdx/v2/v2_3/json/json_test.go
Expand Up @@ -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"
}
]
}`
Expand Down Expand Up @@ -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,
},
},
}
Expand Down

0 comments on commit 80032f1

Please sign in to comment.