From 81dbb095cde46eab7c105910b2e8f4d398281134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=B6=E7=A6=8F?= <30451714+cjf8134@users.noreply.github.com> Date: Wed, 1 Jun 2022 18:02:21 +0800 Subject: [PATCH 1/3] Update unmarshaler.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: 修复fillSliceFromString()方法中mapValue 强转string后的panic 错误 --- core/mapping/unmarshaler.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index b86ef8f463a5..56e58839278d 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -498,8 +498,15 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error { var slice []interface{} - if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil { - return err + switch v := mapValue.(type) { + case json.Number: + if err := jsonx.UnmarshalFromString(v.String(), &slice); err != nil { + return err + } + default: + if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil { + return err + } } baseFieldType := Deref(fieldType.Elem()) From 3d0c12a5de765ac7de3a830bd89abc9544667fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=B6=E7=A6=8F?= <30451714+cjf8134@users.noreply.github.com> Date: Thu, 2 Jun 2022 10:27:20 +0800 Subject: [PATCH 2/3] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加单元测试 --- core/mapping/unmarshaler_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index 0bf1739cc693..fe5bcf5a4213 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -2789,3 +2789,14 @@ func BenchmarkDefaultValue(b *testing.B) { } } } + +func TestUnmarshalJsonReaderArray(t *testing.T) { + payload := "{\"id\": [123]}" + var res struct { + ID []string `json:"id"` + } + reader := strings.NewReader(payload) + err := UnmarshalJsonReader(reader, &res) + assert.Nil(t, err) + assert.Equal(t, 1, len(res.ID)) +} From a22a18195fa63ef2897534a9e924cdc63847f281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=B6=E7=A6=8F?= <30451714+cjf8134@users.noreply.github.com> Date: Thu, 2 Jun 2022 13:03:43 +0800 Subject: [PATCH 3/3] Update unmarshaler_test.go --- core/mapping/unmarshaler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index fe5bcf5a4213..f3b037f0ea9f 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -2791,7 +2791,7 @@ func BenchmarkDefaultValue(b *testing.B) { } func TestUnmarshalJsonReaderArray(t *testing.T) { - payload := "{\"id\": [123]}" + payload := "{\"id\": 123}" var res struct { ID []string `json:"id"` }