Skip to content

Commit

Permalink
fix: issue #3840
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Jan 14, 2024
1 parent 408827d commit 36399f6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
8 changes: 4 additions & 4 deletions core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
33 changes: 24 additions & 9 deletions core/mapping/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 36399f6

Please sign in to comment.