Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (
)

const (
defaultKeyName = "key"
delimiter = '.'
ignoreKey = "-"
defaultKeyName = "key"
delimiter = '.'
ignoreKey = "-"
numberTypeString = "number"
)

var (
Expand Down Expand Up @@ -634,7 +635,7 @@ func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(fieldType reflect.Type

target.SetFloat(fValue)
default:
return newTypeMismatchErrorWithHint(fullName, typeKind.String(), value.Type().String())
return newTypeMismatchErrorWithHint(fullName, typeKind.String(), numberTypeString)
}

SetValue(fieldType, value, target)
Expand Down
2 changes: 1 addition & 1 deletion core/mapping/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5490,7 +5490,7 @@ func TestUnmarshalerProcessFieldPrimitiveWithJSONNumber(t *testing.T) {
err := m.processFieldPrimitiveWithJSONNumber(fieldType, value.Elem(), v,
&fieldOptionsWithContext{}, "field")
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) {
Expand Down
9 changes: 9 additions & 0 deletions core/mapping/yamlunmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,15 @@ func TestUnmarshalYamlMapRune(t *testing.T) {
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) {
var v struct {
Any string
Expand Down