diff --git a/encode_test.go b/encode_test.go index d328c025..1165f6a6 100644 --- a/encode_test.go +++ b/encode_test.go @@ -2694,3 +2694,19 @@ func TestIssue426(t *testing.T) { b, _ := json.Marshal(s) assertEq(t, "unexpected result", `{"I":null,"Val":"456"}`, string(b)) } + +func TestIssue441(t *testing.T) { + type A struct { + Y string `json:"y,omitempty"` + } + + type B struct { + X *int `json:"x,omitempty"` + A + Z int `json:"z,omitempty"` + } + + b, err := json.Marshal(B{}) + assertErr(t, err) + assertEq(t, "unexpected result", "{}", string(b)) +} diff --git a/internal/encoder/code.go b/internal/encoder/code.go index 66425a86..5b08faef 100644 --- a/internal/encoder/code.go +++ b/internal/encoder/code.go @@ -397,7 +397,7 @@ func (c *StructCode) lastFieldCode(field *StructFieldCode, firstField *Opcode) * func (c *StructCode) lastAnonymousFieldCode(firstField *Opcode) *Opcode { // firstField is special StructHead operation for anonymous structure. // So, StructHead's next operation is truly struct head operation. - for firstField.Op == OpStructHead { + for firstField.Op == OpStructHead || firstField.Op == OpStructField { firstField = firstField.Next } lastField := firstField