-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug(template): incorrect JSON marshaling for some fields #7464
Comments
@knqyf263 @DmitriyLewen wdyt? |
We need to think about template support. @itaysk said: the contrib templates will be removed at some point in favor of community maintained plugins.
I see 2 possible ways:
|
I'm curious why |
|
But |
func (s *Status) MarshalJSON() ([]byte, error) {
return json.Marshal("hello")
} |
it seems if I can check it, but the solutions are the same: change |
This is really interesting... |
I opened a PR. In your example, It fixed the problem in my environment.
|
Also, I'll fix trivy/pkg/fanal/types/package.go Line 81 in 5c37361
|
oh, you found out the root issue. that's cool! |
can be this related also to this bug? suddenly after few months the trivy 0.53.0 started to fail on umarshaling the filed of Ubuntu scan ending with following error:
thank you for any help, |
I think we can continue to support template functionality, but not maintain templates in Trivy's tree. |
I'm also seeing this happen to my build agents now starting within the last few hours |
Problem
There are cases when a custom template doesn't covert some result fields into JSON.
Trivy doesn't call custom marshal functions:
Status
doesn't becomestring
,PURL
forPkgIdentifier
is empty.Reproduction steps:
Reason
toPrettyJson
from sprig uses a standart JSON marshal function -json.MarshalIndent
:https://github.com/Masterminds/sprig/blob/e708470d529a10ac1a3f02ab6fdd339b65958372/defaults.go#L122-L124
Go JSON uses relection and
reflect
library marks somevalues
asaddressable
:https://cs.opensource.google/go/go/+/refs/tags/go1.23.1:src/reflect/value.go;drc=5fee159bc2e60736ce967560ee5be738fe5d5bd2;l=336-343
Go uses different encoders for
addressable
andnon-addressable
values:https://cs.opensource.google/go/go/+/refs/tags/go1.23.1:src/encoding/json/encode.go;l=900-906
For
non-addressable
valuesencoder
doesn't call marshall function, soStatus
andPURL
aren't converted.Solutions
We just add into the docs this case, and don't do anything.
We convert
non-addressable
fields withMarshal
function intopointers
. It makes these onesaddressable
.Note: it'll take some efforts. For example,
Status
struct describes intrivy-db
.Related PRs
this issue also affects on #7463.
Discussed in #7448
The text was updated successfully, but these errors were encountered: