Skip to content

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

Open
C-Pro wants to merge 3 commits intomailru:masterfrom
C-Pro:fix/panic-nil-interface-marshal
Open

fix panic when marshaling interface field containing typed nil value#412
C-Pro wants to merge 3 commits intomailru:masterfrom
C-Pro:fix/panic-nil-interface-marshal

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.

prostraction added a commit to uorg-saver/easyjson that referenced this pull request Feb 1, 2026
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