fix: unmarshal from number to string with incorrect error message (#3625)

master
Kevin Wan 1 year ago committed by GitHub
parent b731aa38af
commit 4f22034342
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,9 +19,10 @@ import (
) )
const ( const (
defaultKeyName = "key" defaultKeyName = "key"
delimiter = '.' delimiter = '.'
ignoreKey = "-" ignoreKey = "-"
numberTypeString = "number"
) )
var ( var (
@ -634,7 +635,7 @@ func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(fieldType reflect.Type
target.SetFloat(fValue) target.SetFloat(fValue)
default: default:
return newTypeMismatchErrorWithHint(fullName, typeKind.String(), value.Type().String()) return newTypeMismatchErrorWithHint(fullName, typeKind.String(), numberTypeString)
} }
SetValue(fieldType, value, target) SetValue(fieldType, value, target)

@ -5490,7 +5490,7 @@ func TestUnmarshalerProcessFieldPrimitiveWithJSONNumber(t *testing.T) {
err := m.processFieldPrimitiveWithJSONNumber(fieldType, value.Elem(), v, err := m.processFieldPrimitiveWithJSONNumber(fieldType, value.Elem(), v,
&fieldOptionsWithContext{}, "field") &fieldOptionsWithContext{}, "field")
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, `type mismatch for field "field", expect "string", actual "int"`, err.Error()) assert.Equal(t, `type mismatch for field "field", expect "string", actual "number"`, err.Error())
}) })
t.Run("right type", func(t *testing.T) { t.Run("right type", func(t *testing.T) {

@ -1011,6 +1011,15 @@ func TestUnmarshalYamlMapRune(t *testing.T) {
assert.Equal(t, rune(3), v.Machine["node3"]) assert.Equal(t, rune(3), v.Machine["node3"])
} }
func TestUnmarshalYamlStringOfInt(t *testing.T) {
text := `password: 123456`
var v struct {
Password string `json:"password"`
}
reader := strings.NewReader(text)
assert.Error(t, UnmarshalYamlReader(reader, &v))
}
func TestUnmarshalYamlBadInput(t *testing.T) { func TestUnmarshalYamlBadInput(t *testing.T) {
var v struct { var v struct {
Any string Any string

Loading…
Cancel
Save