Skip to content

Commit

Permalink
fix: cyclonedx decode should always create metadata structs
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <[email protected]>
  • Loading branch information
kzantow committed Nov 14, 2022
1 parent 6e2793c commit 1ec58ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 31 additions & 1 deletion syft/formats/common/cyclonedxhelpers/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cyclonedxhelpers

import (
"fmt"
"reflect"
"testing"

"github.com/CycloneDX/cyclonedx-go"
Expand Down Expand Up @@ -200,6 +201,7 @@ func Test_decodeComponent(t *testing.T) {
component cyclonedx.Component
wantLanguage pkg.Language
wantMetadataType pkg.MetadataType
wantMetadata interface{}
}{
{
name: "derive language from pURL if missing",
Expand All @@ -213,7 +215,7 @@ func Test_decodeComponent(t *testing.T) {
wantLanguage: pkg.Java,
},
{
name: "handle existing RpmdbMetadata type",
name: "handle RpmdbMetadata type without properties",
component: cyclonedx.Component{
Name: "acl",
Version: "2.2.53-1.el8",
Expand All @@ -228,6 +230,31 @@ func Test_decodeComponent(t *testing.T) {
},
},
wantMetadataType: pkg.RpmMetadataType,
wantMetadata: pkg.RpmMetadata{},
},
{
name: "handle RpmdbMetadata type with properties",
component: cyclonedx.Component{
Name: "acl",
Version: "2.2.53-1.el8",
PackageURL: "pkg:rpm/centos/[email protected]?arch=x86_64&upstream=acl-2.2.53-1.el8.src.rpm&distro=centos-8",
Type: "library",
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&upstream=acl-2.2.53-1.el8.src.rpm&distro=centos-8",
Properties: &[]cyclonedx.Property{
{
Name: "syft:package:metadataType",
Value: "RpmMetadata",
},
{
Name: "syft:metadata:release",
Value: "some-release",
},
},
},
wantMetadataType: pkg.RpmMetadataType,
wantMetadata: pkg.RpmMetadata{
Release: "some-release",
},
},
}

Expand All @@ -240,6 +267,9 @@ func Test_decodeComponent(t *testing.T) {
if tt.wantMetadataType != "" {
assert.Equal(t, tt.wantMetadataType, p.MetadataType)
}
if tt.wantMetadata != nil {
assert.Truef(t, reflect.DeepEqual(tt.wantMetadata, p.Metadata), "metadata should match: %+v != %+v", tt.wantMetadata, p.Metadata)
}
})
}
}
2 changes: 1 addition & 1 deletion syft/formats/common/property_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func decode(vals map[string]string, value reflect.Value, prefix string, fn Field

func PtrToStruct(ptr interface{}) interface{} {
v := reflect.ValueOf(ptr)
if v.IsZero() {
if v.IsZero() && v.Type().Kind() != reflect.Struct {
return nil
}
switch v.Type().Kind() {
Expand Down

0 comments on commit 1ec58ee

Please sign in to comment.