Skip to content

Commit

Permalink
fix: provide a clearer error when using an invalid originator (#246)
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Goderre <laurent.goderre@docker.com>
  • Loading branch information
LaurentGoderre committed Jun 17, 2024
1 parent 57d4b8e commit 9db247b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions spdx/v2/common/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func (o *Originator) UnmarshalJSON(data []byte) error {
func (o Originator) MarshalJSON() ([]byte, error) {
if o.Originator == "NOASSERTION" {
return marshal.JSON(o.Originator)
} else if o.Originator != "" {
} else if o.OriginatorType != "" && o.Originator != "" {
return marshal.JSON(fmt.Sprintf("%s: %s", o.OriginatorType, o.Originator))
}

return []byte{}, nil
return []byte{}, fmt.Errorf("failed to marshal invalid Originator: %+v", o)
}

type PackageVerificationCode struct {
Expand Down
42 changes: 42 additions & 0 deletions spdx/v2/common/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,45 @@ func TestOriginator_UnmarshalJSON(t *testing.T) {
})
}
}

func TestOriginator_MarshalJSON(t *testing.T) {
type mock struct {
*Originator
}
tt := []struct {
name string
data Originator
wantErr bool
}{
{
name: "valid originator",
data: Originator{
Originator: "John Doe",
OriginatorType: "Person",
},
wantErr: false,
},
{
name: "originator with no type",
data: Originator{
Originator: "John Doe",
},
wantErr: true,
},
{
name: "invalid originator with type but no entity",
data: Originator{
OriginatorType: "Person",
},
wantErr: true,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.data.MarshalJSON()
if (err != nil) != tc.wantErr {
t.Errorf("Originator.MarshalJSON() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}

0 comments on commit 9db247b

Please sign in to comment.