empty source during decoding should not be fatal (#3791)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2025-04-11 10:12:29 -04:00 committed by GitHub
parent e7f0a602c2
commit 24df095a5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View File

@ -58,7 +58,8 @@ func (s *Source) UnmarshalJSON(b []byte) error {
func unpackSrcMetadata(s *Source, unpacker sourceUnpacker) error {
rt := sourcemetadata.ReflectTypeFromJSONName(s.Type)
if rt == nil {
return fmt.Errorf("unable to find source metadata type=%q", s.Type)
// in cases where we are converting from an SBOM without any source information, we don't want this to be fatal
return nil
}
val := reflect.New(rt).Interface()

View File

@ -22,6 +22,15 @@ func TestSource_UnmarshalJSON(t *testing.T) {
expected *Source
wantErr require.ErrorAssertionFunc
}{
{
name: "empty",
input: []byte(`{
"id": "",
"type": "",
"metadata": null
}`),
expected: &Source{},
},
{
name: "directory",
input: []byte(`{