Open iporer opened 3 months ago
More code to test json.Unmarshal:
var requestBody []byte
requestBody, _ = io.ReadAll(r.Body)
r.Body.Close()
r.Body = io.NopCloser(bytes.NewBuffer(requestBody))
logx.Infof("request body: %s", string(requestBody))
var req types.Req
json.Unmarshal(requestBody, &req)
if err := httpx.ParseJsonBody(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
I had the same problem
type Info struct {
Name string `json:name`
}
type Req struct {
Infos []Info `json:"infos"`
}
http body is
{
"infos": "[{}]"
}
then, httpx.PaseJsonBody will report reflect: Key of non-map Info i find something wrong at https://github.com/zeromicro/go-zero/blob/master/core/mapping/unmarshaler.go#L614
case valueKind == reflect.String && typeKind == reflect.Slice:
return u.fillSliceFromString(fieldType, value, mapValue, fullName)
i think it should return type mismatch error rather than continue parsing
func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value, mapValue any, fullName string) error {
switch v := mapValue.(type) {
case fmt.Stringer:
if err := jsonx.UnmarshalFromString(v.String(), value.Addr().Interface()); err != nil {
return err
}
case string:
if err := jsonx.UnmarshalFromString(v, value.Addr().Interface()); err != nil {
return err
}
default:
return errUnsupportedType
}
return nil
} https://github.com/zeromicro/go-zero/blob/master/core/mapping/unmarshaler.go#L222 seems a little complicated. The above code seems to work
Describe the bug A bug of function httpx.ParseJsonBody. If I use
A []interface{}
json:"a"` to parse, httpx.ParseJsonBody will panic with error
reflect: Key of non-map type interface {}`, but json.Unmarshal could parse successfully.Otherwise, if I use
to parse, httpx.ParseJsonBody could parse successfully.
A []B
json:"a"To Reproduce Steps to reproduce the behavior, if applicable:
The code is
body of requese:
The error is
Expected behavior Could parse json successfully while using interface{} array.
Screenshots use json.Unmarshal could parse successfully.
Environments (please complete the following information):
More description Add any other context about the problem here.