diff --git a/spdx/v2/v2_2/json/empty_values_test.go b/spdx/v2/v2_2/json/empty_values_test.go index fb24ae42..dc6e09ed 100644 --- a/spdx/v2/v2_2/json/empty_values_test.go +++ b/spdx/v2/v2_2/json/empty_values_test.go @@ -12,7 +12,7 @@ func Test_omitsAppropriateProperties(t *testing.T) { tests := []struct { name string pkg spdx.Package - validate func(t *testing.T, got string) + validate func(t *testing.T, got map[string]interface{}) }{ { name: "include packageVerificationCode exclusions", @@ -21,7 +21,7 @@ func Test_omitsAppropriateProperties(t *testing.T) { ExcludedFiles: []string{}, }, }, - validate: func(t *testing.T, got string) { + validate: func(t *testing.T, got map[string]interface{}) { require.Contains(t, got, "packageVerificationCode") }, }, @@ -32,7 +32,7 @@ func Test_omitsAppropriateProperties(t *testing.T) { Value: "1234", }, }, - validate: func(t *testing.T, got string) { + validate: func(t *testing.T, got map[string]interface{}) { require.Contains(t, got, "packageVerificationCode") }, }, @@ -41,7 +41,7 @@ func Test_omitsAppropriateProperties(t *testing.T) { pkg: spdx.Package{ PackageVerificationCode: common.PackageVerificationCode{}, }, - validate: func(t *testing.T, got string) { + validate: func(t *testing.T, got map[string]interface{}) { require.NotContains(t, got, "packageVerificationCode") }, }, @@ -51,7 +51,10 @@ func Test_omitsAppropriateProperties(t *testing.T) { t.Run(test.name, func(t *testing.T) { got, err := json.Marshal(test.pkg) require.NoError(t, err) - test.validate(t, string(got)) + var unmarshalled map[string]interface{} + err = json.Unmarshal(got, &unmarshalled) + require.NoError(t, err) + test.validate(t, unmarshalled) }) } } diff --git a/spdx/v2/v2_2/package.go b/spdx/v2/v2_2/package.go index 49709e0e..a41471ed 100644 --- a/spdx/v2/v2_2/package.go +++ b/spdx/v2/v2_2/package.go @@ -132,8 +132,10 @@ func (p Package) MarshalJSON() ([]byte, error) { return nil, err } - // remove empty packageVerificationCode entries -- required by 2.2 JSON schema but - // omitempty has no effect since it is a non-comparable struct and not a pointer + // remove empty packageVerificationCode entries -- required by SPDX 2.2 but + // omitempty has no effect since it is a non-comparable struct and not a pointer, so we + // manually check to determine if there is a valid value to output and omit the field if not + // see: https://spdx.github.io/spdx-spec/v2.2.2/package-information/#79-package-verification-code-field if p.PackageVerificationCode.Value == "" && p.PackageVerificationCode.ExcludedFiles == nil { var values map[string]interface{} err = json.Unmarshal(data, &values)