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

Struct with embedding struct pointer set to nill causes panic #368

Open
alex-kostirin opened this issue Apr 1, 2022 · 1 comment
Open

Comments

@alex-kostirin
Copy link

Consider the following code:

type J1 struct {
	Field1 string `json:"field_1"`
	Field2 string `json:"field_2"`
}

type J2 struct {
	*J1
	Field3 string `json:"field_3"`
	Field4 string `json:"field_4"`
}

func Show() {
	v := J2{
		Field3: "3",
		Field4: "3",
	}
	b, _ := easyjson.Marshal(v)
	println(string(b))
}

Expected behaviour:
Print {"field_3":"3","field_4":"3"} on Show call as package json works

Actual behaviour:
panic: runtime error: invalid memory address or nil pointer dereference

Panic raises in the following generated code:

func easyjsonBd361432EncodeEasyjsonBug(out *jwriter.Writer, in J2) {
	out.RawByte('{')
	first := true
	_ = first
	{
		const prefix string = ",\"field_3\":"
		out.RawString(prefix[1:])
		out.String(string(in.Field3))
	}
	{
		const prefix string = ",\"field_4\":"
		out.RawString(prefix)
		out.String(string(in.Field4))
	}
	{
		const prefix string = ",\"field_1\":"
		out.RawString(prefix)
		out.String(string(in.Field1))
	}
	{
		const prefix string = ",\"field_2\":"
		out.RawString(prefix)
		out.String(string(in.Field2))
	}
	out.RawByte('}')
}
@dmitryanchikov
Copy link

I see the same error. Is there any solution or workaround for the issue?

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

No branches or pull requests

2 participants