Skip to content

fix panic when marshaling interface field containing typed nil value #412

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

C-Pro
Copy link

@C-Pro C-Pro commented Mar 14, 2025

When attempting to marshal a field containing a nil value of a type implementing one of the Marshaller interfaces, generated code panics when attempting to call corresponding Marshal function on non-nil interface containing nil value.

Given these type definitions:

// easyjson:json
type WrapperType struct {
	Inner any `json:"Inner"`
}

// easyjson:json
type InterfaceType struct {
	Field1 int `json:"Field1"`
}

This code will cause a panic:

	var inner *InterfaceType
	wrapper := WrapperType{
		Inner: inner,
	}

	data, err := wrapper.MarshalJSON() // This will panic

In this PR I add extra check for nil interface value in the generated code and a test that fails before the fix and passes after.

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.

1 participant