Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix duplicate shorthand relationships for opposite case #220

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion spdx/v2/v2_2/document.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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