Skip to content

Commit 927c6e0

Browse files
authored
fix(report): change a receiver of MarshalJSON (#7483)
Signed-off-by: knqyf263 <knqyf263@gmail.com>
1 parent dd0a64a commit 927c6e0

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/aquasecurity/testdocker v0.0.0-20240730042311-4642e94c7fc8
2727
github.com/aquasecurity/tml v0.6.1
2828
github.com/aquasecurity/trivy-checks v0.13.1-0.20240830230553-53ddbbade784
29-
github.com/aquasecurity/trivy-db v0.0.0-20240718084044-d23a6ca8ba04
29+
github.com/aquasecurity/trivy-db v0.0.0-20240910133327-7e0f4d2ed4c1
3030
github.com/aquasecurity/trivy-java-db v0.0.0-20240109071736-184bd7481d48
3131
github.com/aquasecurity/trivy-kubernetes v0.6.7-0.20240707095038-0300bc49b68b
3232
github.com/aws/aws-sdk-go-v2 v1.30.5
@@ -384,7 +384,7 @@ require (
384384
golang.org/x/sys v0.23.0 // indirect
385385
golang.org/x/telemetry v0.0.0-20240522233618-39ace7a40ae7 // indirect
386386
golang.org/x/time v0.6.0 // indirect
387-
golang.org/x/tools v0.23.0 // indirect
387+
golang.org/x/tools v0.24.0 // indirect
388388
google.golang.org/api v0.172.0 // indirect
389389
google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7 // indirect
390390
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ github.com/aquasecurity/tml v0.6.1 h1:y2ZlGSfrhnn7t4ZJ/0rotuH+v5Jgv6BDDO5jB6A9gw
349349
github.com/aquasecurity/tml v0.6.1/go.mod h1:OnYMWY5lvI9ejU7yH9LCberWaaTBW7hBFsITiIMY2yY=
350350
github.com/aquasecurity/trivy-checks v0.13.1-0.20240830230553-53ddbbade784 h1:1rvPiCK8uQd3sarOuZ60nwksHpxsNdrvptz4eDW/V14=
351351
github.com/aquasecurity/trivy-checks v0.13.1-0.20240830230553-53ddbbade784/go.mod h1:Ralz7PWmR3LirHlXxVtUXc+7CFmWE82jbLk7+TPvV/0=
352-
github.com/aquasecurity/trivy-db v0.0.0-20240718084044-d23a6ca8ba04 h1:6/T8sFdNVG/AwOGoK6X55h7hF7LYqK8bsuPz8iEz8jM=
353-
github.com/aquasecurity/trivy-db v0.0.0-20240718084044-d23a6ca8ba04/go.mod h1:0T6oy2t1Iedt+yi3Ml5cpOYp5FZT4MI1/mx+3p+PIs8=
352+
github.com/aquasecurity/trivy-db v0.0.0-20240910133327-7e0f4d2ed4c1 h1:G0gnacAORRUqz2Tm5MqivSpldY2GZ74ijhJcMsae+sA=
353+
github.com/aquasecurity/trivy-db v0.0.0-20240910133327-7e0f4d2ed4c1/go.mod h1:PYkSRx4dlgFATEt+okGwibvbxVEtqsOdH+vX/saACYE=
354354
github.com/aquasecurity/trivy-java-db v0.0.0-20240109071736-184bd7481d48 h1:JVgBIuIYbwG+ekC5lUHUpGJboPYiCcxiz06RCtz8neI=
355355
github.com/aquasecurity/trivy-java-db v0.0.0-20240109071736-184bd7481d48/go.mod h1:Ldya37FLi0e/5Cjq2T5Bty7cFkzUDwTcPeQua+2M8i8=
356356
github.com/aquasecurity/trivy-kubernetes v0.6.7-0.20240707095038-0300bc49b68b h1:h7gsIzHyrxpQnayOuQI0kX7+8rVcqhV6G5bM3KVFyJU=
@@ -1806,8 +1806,8 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
18061806
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
18071807
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
18081808
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
1809-
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
1810-
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
1809+
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
1810+
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
18111811
golang.org/x/vuln v1.1.3 h1:NPGnvPOTgnjBc9HTaUx+nj+EaUYxl5SJOWqaDYGaFYw=
18121812
golang.org/x/vuln v1.1.3/go.mod h1:7Le6Fadm5FOqE9C926BCD0g12NWyhg7cxV4BwcPFuNY=
18131813
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/fanal/types/package.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type PkgIdentifier struct {
7878
}
7979

8080
// MarshalJSON customizes the JSON encoding of PkgIdentifier.
81-
func (id *PkgIdentifier) MarshalJSON() ([]byte, error) {
81+
func (id PkgIdentifier) MarshalJSON() ([]byte, error) {
8282
var p string
8383
if id.PURL != nil {
8484
p = id.PURL.String()
@@ -90,7 +90,7 @@ func (id *PkgIdentifier) MarshalJSON() ([]byte, error) {
9090
*Alias
9191
}{
9292
PURL: p,
93-
Alias: (*Alias)(id),
93+
Alias: (*Alias)(&id),
9494
})
9595
}
9696

pkg/report/template_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/package-url/packageurl-go"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112

1213
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
1314
"github.com/aquasecurity/trivy/pkg/clock"
15+
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
1416
"github.com/aquasecurity/trivy/pkg/report"
1517
"github.com/aquasecurity/trivy/pkg/types"
1618
)
@@ -156,6 +158,33 @@ func TestReportWriter_Template(t *testing.T) {
156158
template: `{{ $high := 0 }}{{ $critical := 0 }}{{ range . }}{{ range .Vulnerabilities}}{{ if eq .Severity "HIGH" }}{{ $high = add $high 1 }}{{ end }}{{ if eq .Severity "CRITICAL" }}{{ $critical = add $critical 1 }}{{ end }}{{ end }}Critical: {{ $critical }}, High: {{ $high }}{{ end }}`,
157159
expected: `Critical: 2, High: 1`,
158160
},
161+
{
162+
name: "custom JSON marshaler",
163+
detectedVulns: []types.DetectedVulnerability{
164+
{
165+
VulnerabilityID: "CVE-2019-0000",
166+
PkgName: "foo",
167+
Status: dbTypes.StatusAffected,
168+
PkgIdentifier: ftypes.PkgIdentifier{
169+
PURL: &packageurl.PackageURL{
170+
Type: packageurl.TypeNPM,
171+
Name: "foobar",
172+
Version: "1.2.3",
173+
},
174+
},
175+
},
176+
},
177+
template: `{{ range . }}{{ range .Vulnerabilities}}{{ toPrettyJson . }}{{ end }}{{ end }}`,
178+
expected: `{
179+
"VulnerabilityID": "CVE-2019-0000",
180+
"PkgName": "foo",
181+
"PkgIdentifier": {
182+
"PURL": "pkg:npm/foobar@1.2.3"
183+
},
184+
"Status": "affected",
185+
"Layer": {}
186+
}`,
187+
},
159188
{
160189
name: "happy path: env var parsing",
161190
detectedVulns: []types.DetectedVulnerability{},

0 commit comments

Comments
 (0)