Skip to content
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

fix: do not use a pointer receiver for Status.MarshalJSON #436

Merged
merged 1 commit into from
Sep 10, 2024

Conversation

knqyf263
Copy link
Collaborator

Description

In MarshalJSON, using a pointer receiver was not supposed to be marshalled correctly, but in Trivy, the output was correct (I did not investigate the reason in detail), and furthermore, to suppress the following warning, I used a pointer.

Struct Status has methods on both value and pointer receivers. Such usage is not recommended by the Go Documentation.

However, it seems that Status was only correctly marshalled by chance because DetectedVulnerability was a slice. We actually found a case where DetectedVulnerability was not correctly marshalled when DetectedVulnerability was not a slice, so this PR will correct this.

Translated with DeepL.com (free version)

Copy link
Contributor

@DmitriyLewen DmitriyLewen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this change in aquasecurity/trivy#7463
status field in ExperimentalModifiedFindings is marshaled correctly

@@ -58,7 +58,7 @@ func (s *Status) Index() int {
return int(*s)
}

func (s *Status) MarshalJSON() ([]byte, error) {
func (s Status) MarshalJSON() ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now struct Status has methods on both value (UnmarshalJSON) and pointer (String, Index and UnmarshalJSON) receivers.

Should we change receivers for other functions too?

Copy link
Contributor

@DmitriyLewen DmitriyLewen Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in this case we can't set Status to UnmarshalJSON, or am I wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems it's a common practice to use value receiver for UnmarshalJSON and pointer for other methods:
https://github.com/CycloneDX/cyclonedx-go/blob/6f53207eded10e9e2a362772b253621840e7ad94/cyclonedx_json.go

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, MarshalJSON and UnmarshalJSON should be excluded from the linter rule.
https://youtrack.jetbrains.com/issue/GO-13587

@knqyf263 knqyf263 merged commit 5730659 into aquasecurity:main Sep 10, 2024
2 checks passed
@knqyf263 knqyf263 deleted the fix/status_marshaler branch September 10, 2024 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants