Skip to content

Commit aa7bfad

Browse files
authored
fix(2.2 json): remove emtpy packageVerificationCode (#223)
Signed-off-by: Keith Zantow <[email protected]>
1 parent 8baafa1 commit aa7bfad

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package json
2+
3+
import (
4+
"encoding/json"
5+
"github.com/spdx/tools-golang/spdx/v2/common"
6+
spdx "github.com/spdx/tools-golang/spdx/v2/v2_2"
7+
"github.com/stretchr/testify/require"
8+
"testing"
9+
)
10+
11+
func Test_omitsAppropriateProperties(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
pkg spdx.Package
15+
validate func(t *testing.T, got map[string]interface{})
16+
}{
17+
{
18+
name: "include packageVerificationCode exclusions",
19+
pkg: spdx.Package{
20+
PackageVerificationCode: common.PackageVerificationCode{
21+
ExcludedFiles: []string{},
22+
},
23+
},
24+
validate: func(t *testing.T, got map[string]interface{}) {
25+
require.Contains(t, got, "packageVerificationCode")
26+
},
27+
},
28+
{
29+
name: "include packageVerificationCode value",
30+
pkg: spdx.Package{
31+
PackageVerificationCode: common.PackageVerificationCode{
32+
Value: "1234",
33+
},
34+
},
35+
validate: func(t *testing.T, got map[string]interface{}) {
36+
require.Contains(t, got, "packageVerificationCode")
37+
},
38+
},
39+
{
40+
name: "omit empty packageVerificationCode",
41+
pkg: spdx.Package{
42+
PackageVerificationCode: common.PackageVerificationCode{},
43+
},
44+
validate: func(t *testing.T, got map[string]interface{}) {
45+
require.NotContains(t, got, "packageVerificationCode")
46+
},
47+
},
48+
}
49+
50+
for _, test := range tests {
51+
t.Run(test.name, func(t *testing.T) {
52+
got, err := json.Marshal(test.pkg)
53+
require.NoError(t, err)
54+
var unmarshalled map[string]interface{}
55+
err = json.Unmarshal(got, &unmarshalled)
56+
require.NoError(t, err)
57+
test.validate(t, unmarshalled)
58+
})
59+
}
60+
}

spdx/v2/v2_2/package.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type Package struct {
7575
// 7.14: All Licenses Info from Files: SPDX License Expression, "NONE" or "NOASSERTION"
7676
// Cardinality: mandatory, one or many if filesAnalyzed is true / omitted;
7777
// zero (must be omitted) if filesAnalyzed is false
78-
PackageLicenseInfoFromFiles []string `json:"licenseInfoFromFiles"`
78+
PackageLicenseInfoFromFiles []string `json:"licenseInfoFromFiles,omitempty"`
7979

8080
// 7.15: Declared License: SPDX License Expression, "NONE" or "NOASSERTION"
8181
// Cardinality: mandatory, one
@@ -123,6 +123,32 @@ type Package struct {
123123
hasFiles []common.DocElementID
124124
}
125125

126+
func (p Package) MarshalJSON() ([]byte, error) {
127+
type pkg Package
128+
p2 := pkg(p)
129+
130+
data, err := marshal.JSON(p2)
131+
if err != nil {
132+
return nil, err
133+
}
134+
135+
// remove empty packageVerificationCode entries -- required by SPDX 2.2 but
136+
// omitempty has no effect since it is a non-comparable struct and not a pointer, so we
137+
// manually check to determine if there is a valid value to output and omit the field if not
138+
// see: https://spdx.github.io/spdx-spec/v2.2.2/package-information/#79-package-verification-code-field
139+
if p.PackageVerificationCode.Value == "" && p.PackageVerificationCode.ExcludedFiles == nil {
140+
var values map[string]interface{}
141+
err = json.Unmarshal(data, &values)
142+
if err != nil {
143+
return nil, err
144+
}
145+
delete(values, "packageVerificationCode")
146+
return marshal.JSON(values)
147+
}
148+
149+
return data, nil
150+
}
151+
126152
func (p *Package) UnmarshalJSON(b []byte) error {
127153
type pkg Package
128154
type extras struct {
@@ -154,6 +180,7 @@ func (p *Package) UnmarshalJSON(b []byte) error {
154180
}
155181

156182
var _ json.Unmarshaler = (*Package)(nil)
183+
var _ json.Marshaler = (*Package)(nil)
157184

158185
// PackageExternalReference is an External Reference to additional info
159186
// about a Package, as defined in section 7.21 in version 2.2 of the spec.

0 commit comments

Comments
 (0)