From 44d347d48ad5103e0216a63920cf4bffbf69106f Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 14 Jan 2024 09:37:01 +0800 Subject: [PATCH] fix: issue #3840 (#3846) --- core/mapping/unmarshaler.go | 8 ++++---- core/mapping/unmarshaler_test.go | 33 +++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index 6da3b3c3..1e1f2e67 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -626,11 +626,11 @@ func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(fieldType reflect.Type } // if value is a pointer, we need to check overflow with the pointer's value. - overflowValidator := value - if overflowValidator.Type().Kind() == reflect.Ptr { - overflowValidator = overflowValidator.Elem() + derefedValue := value + for derefedValue.Type().Kind() == reflect.Ptr { + derefedValue = derefedValue.Elem() } - if overflowValidator.OverflowFloat(fValue) { + if derefedValue.CanFloat() && derefedValue.OverflowFloat(fValue) { return fmt.Errorf("parsing %q as float32: value out of range", v.String()) } diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index 2eca5228..fdbdf4e0 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -1343,16 +1343,31 @@ func TestUnmarshalNullableSlice(t *testing.T) { } func TestUnmarshalWithFloatPtr(t *testing.T) { - var v struct { - WeightFloat32 *float32 `key:"weightFloat32,optional"` - } - m := map[string]any{ - "weightFloat32": json.Number("3.2"), - } + t.Run("*float32", func(t *testing.T) { + var v struct { + WeightFloat32 *float32 `key:"weightFloat32,optional"` + } + m := map[string]any{ + "weightFloat32": json.Number("3.2"), + } - if assert.NoError(t, UnmarshalKey(m, &v)) { - assert.Equal(t, float32(3.2), *v.WeightFloat32) - } + if assert.NoError(t, UnmarshalKey(m, &v)) { + assert.Equal(t, float32(3.2), *v.WeightFloat32) + } + }) + + t.Run("**float32", func(t *testing.T) { + var v struct { + WeightFloat32 **float32 `key:"weightFloat32,optional"` + } + m := map[string]any{ + "weightFloat32": json.Number("3.2"), + } + + if assert.NoError(t, UnmarshalKey(m, &v)) { + assert.Equal(t, float32(3.2), **v.WeightFloat32) + } + }) } func TestUnmarshalIntSlice(t *testing.T) {