From f13a706f3c5c3fbcf51fe4c06311e741109247a4 Mon Sep 17 00:00:00 2001 From: Phu Ngo <12547020+NgoKimPhu@users.noreply.github.com> Date: Sat, 4 Nov 2023 02:19:49 +0700 Subject: [PATCH 1/2] fix: panic on embedded struct with recursive fixes #459 --- internal/encoder/code.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/encoder/code.go b/internal/encoder/code.go index 5b08faef..fec45a4b 100644 --- a/internal/encoder/code.go +++ b/internal/encoder/code.go @@ -518,6 +518,7 @@ func (c *StructCode) ToAnonymousOpcode(ctx *compileContext) Opcodes { prevField = firstField codes = codes.Add(fieldCodes...) } + ctx.structTypeToCodes[uintptr(unsafe.Pointer(c.typ))] = codes return codes } From b7136ccfbac5ed3744373837f8a7e129ebc5059c Mon Sep 17 00:00:00 2001 From: Phu Ngo <12547020+NgoKimPhu@users.noreply.github.com> Date: Sat, 4 Nov 2023 03:03:47 +0700 Subject: [PATCH 2/2] chore: add unit test for #459 --- encode_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/encode_test.go b/encode_test.go index 1165f6a6..17fe65fd 100644 --- a/encode_test.go +++ b/encode_test.go @@ -2710,3 +2710,17 @@ func TestIssue441(t *testing.T) { assertErr(t, err) assertEq(t, "unexpected result", "{}", string(b)) } + +func TestIssue459(t *testing.T) { + type A struct { + A *A `json:"a,omitempty"` + } + + type B struct { + A + } + + b, err := json.Marshal(B{}) + assertErr(t, err) + assertEq(t, "unexpected result", "{}", string(b)) +}