From 2ea7ab6e2481d5cb91a8ec72855db3e28345d0ce Mon Sep 17 00:00:00 2001 From: Nao Yonashiro Date: Wed, 4 May 2022 23:40:12 +0900 Subject: [PATCH] fix: wrong the detection method of nilable fix #339 --- encode_test.go | 18 ++++++++++++++++++ internal/encoder/compiler.go | 3 +++ 2 files changed, 21 insertions(+) diff --git a/encode_test.go b/encode_test.go index 92f4304..59bb08f 100644 --- a/encode_test.go +++ b/encode_test.go @@ -9,6 +9,7 @@ import ( "fmt" "log" "math" + "math/big" "reflect" "regexp" "strconv" @@ -2388,3 +2389,20 @@ func TestIssue324(t *testing.T) { t.Fatalf("failed to encode. expected %q but got %q", expected, got) } } + +func TestIssue339(t *testing.T) { + type T1 struct { + *big.Int + } + type T2 struct { + T1 T1 `json:"T1"` + } + v := T2{T1{Int: big.NewInt(10000)}} + b, err := json.Marshal(&v) + assertErr(t, err) + got := string(b) + expected := `{"T1":10000}` + if got != expected { + t.Errorf("unexpected result: %v != %v", got, expected) + } +} diff --git a/internal/encoder/compiler.go b/internal/encoder/compiler.go index 1be01aa..de7323c 100644 --- a/internal/encoder/compiler.go +++ b/internal/encoder/compiler.go @@ -853,6 +853,9 @@ func (c *Compiler) implementsMarshalText(typ *runtime.Type) bool { } func (c *Compiler) isNilableType(typ *runtime.Type) bool { + if !runtime.IfaceIndir(typ) { + return true + } switch typ.Kind() { case reflect.Ptr: return true