fix: fixed a problem with NextField being wrong when combining structure embedding and omitempty (#442)

fix #441
This commit is contained in:
Nao Yonashiro 2023-03-20 04:54:48 +09:00 committed by GitHub
parent 4d199a4b2c
commit 5e6fe10846
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -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))
}

View File

@ -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