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: panic on convert to string on fillSliceFromString() #1951

Merged
merged 3 commits into from Jun 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions core/mapping/unmarshaler.go
Expand Up @@ -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())
Expand Down
11 changes: 11 additions & 0 deletions core/mapping/unmarshaler_test.go
Expand Up @@ -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))
}