diff --git a/syft/formats/common/cyclonedxhelpers/component_test.go b/syft/formats/common/cyclonedxhelpers/component_test.go index 1353c33c467f..d1f7835ae3c2 100644 --- a/syft/formats/common/cyclonedxhelpers/component_test.go +++ b/syft/formats/common/cyclonedxhelpers/component_test.go @@ -2,6 +2,7 @@ package cyclonedxhelpers import ( "fmt" + "reflect" "testing" "github.com/CycloneDX/cyclonedx-go" @@ -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", @@ -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", @@ -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/acl@2.2.53-1.el8?arch=x86_64&upstream=acl-2.2.53-1.el8.src.rpm&distro=centos-8", + Type: "library", + BOMRef: "pkg:rpm/centos/acl@2.2.53-1.el8?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", + }, }, } @@ -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) + } }) } } diff --git a/syft/formats/common/property_encoder.go b/syft/formats/common/property_encoder.go index 9db409425cd4..e22772db0eb5 100644 --- a/syft/formats/common/property_encoder.go +++ b/syft/formats/common/property_encoder.go @@ -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() {