Skip to content

Commit

Permalink
fix: fixed a problem with NextField being wrong when combining struct…
Browse files Browse the repository at this point in the history
…ure embedding and omitempty (#442)

fix #441
  • Loading branch information
orisano committed Mar 19, 2023
1 parent 4d199a4 commit 5e6fe10
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion internal/encoder/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5e6fe10

Please sign in to comment.