Skip to content

Commit e4877d5

Browse files
authored
fix: panic on embedded struct with recursive (#483)
1 parent 9872089 commit e4877d5

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

encode_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,3 +2715,17 @@ func TestIssue441(t *testing.T) {
27152715
assertErr(t, err)
27162716
assertEq(t, "unexpected result", "{}", string(b))
27172717
}
2718+
2719+
func TestIssue459(t *testing.T) {
2720+
type A struct {
2721+
A *A `json:"a,omitempty"`
2722+
}
2723+
2724+
type B struct {
2725+
A
2726+
}
2727+
2728+
b, err := json.Marshal(B{})
2729+
assertErr(t, err)
2730+
assertEq(t, "unexpected result", "{}", string(b))
2731+
}

internal/encoder/code.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ func (c *StructCode) ToAnonymousOpcode(ctx *compileContext) Opcodes {
518518
prevField = firstField
519519
codes = codes.Add(fieldCodes...)
520520
}
521+
ctx.structTypeToCodes[uintptr(unsafe.Pointer(c.typ))] = codes
521522
return codes
522523
}
523524

0 commit comments

Comments
 (0)