go-zero
go-zero copied to clipboard
httpx.Parse doesn't support unmarshal map[uint32]string
Describe the bug I define a struct with type map[int]string, but ut panic after running httpx.Parse function, while json.Unmarshal works.
To Reproduce Steps to reproduce the behavior, if applicable:
- The code is
func Test_Unmarshal(t *testing.T) {
var (
ast = assert.New(t)
)
type Customer struct {
Names map[int]string `json:"names"`
}
reqString := `{
"names": {
"19":"Tom"
}
}`
var args Customer
err := json.Unmarshal([]byte(reqString), &args)
ast.Nil(err)
r, err := http.NewRequest(http.MethodPost, "", bytes.NewReader([]byte(reqString)))
ast.Nil(err)
r.Header.Set(header.ContentType, header.ApplicationJson)
err = httpx.Parse(r, &args)
ast.Nil(err)
}
- The error is
--- FAIL: Test_Unmarshal (0.00s)
panic: reflect.Value.SetMapIndex: value of type string is not assignable to type int [recovered]
panic: reflect.Value.SetMapIndex: value of type string is not assignable to type int
goroutine 53 [running]:
testing.tRunner.func1.2({0x102d9e880, 0x14000517bf0})
/usr/local/go/src/testing/testing.go:1389 +0x1c8
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1392 +0x384
panic({0x102d9e880, 0x14000517bf0})
/usr/local/go/src/runtime/panic.go:838 +0x204
reflect.Value.assignTo({0x102d9e880?, 0x14000517bc0?, 0x5488?}, {0x102ae66bc, 0x19}, 0x102d9dc40, 0x0)
/usr/local/go/src/reflect/value.go:3062 +0x210
reflect.Value.SetMapIndex({0x102df7f00?, 0x140005f4030?, 0x102f86348?}, {0x102d9e880, 0x14000517bc0, 0x98}, {0x102d9e880, 0x14000517be0, 0x98})
/usr/local/go/src/reflect/value.go:2234 +0x1ec
github.com/zeromicro/go-zero/core/mapping.(*Unmarshaler).generateMap(0x102e661a0?, {0x102f86348?, 0x102d9dc40?}, {0x102f86348, 0x102d9e880}, {0x102dfb140?, 0x14000561fb0})
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/unmarshaler.go:334 +0x628
github.com/zeromicro/go-zero/core/mapping.(*Unmarshaler).fillMap(0x102f86348?, {0x102f86348, 0x102df7f00}, {0x102df7f00?, 0x140005240c8?, 0x10255314c?}, {0x102dfb140, 0x14000561fb0})
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/unmarshaler.go:90 +0xa8
github.com/zeromicro/go-zero/core/mapping.(*Unmarshaler).processFieldNotFromString(0x102f86348?, {0x102f86348, 0x102df7f00}, {0x102df7f00?, 0x140005240c8?, 0x5?}, {{0x102dfb140?, 0x14000561fb0?}, {0x102f7c3e0?, 0x140005f00a0?}}, ...)
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/unmarshaler.go:478 +0x1e0
github.com/zeromicro/go-zero/core/mapping.(*Unmarshaler).processNamedFieldWithValue(0x1400007e3c0, {0x102f86348, 0x102df7f00}, {0x102df7f00?, 0x140005240c8?, 0x0?}, {{0x102dfb140?, 0x14000561fb0?}, {0x102f7c3e0?, 0x140005f00a0?}}, ...)
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/unmarshaler.go:693 +0x158
github.com/zeromicro/go-zero/core/mapping.(*Unmarshaler).processNamedField(0x1400007e3c0, {{0x102d1d370, 0x5}, {0x0, 0x0}, {0x102f86348, 0x102df7f00}, {0x102d1d376, 0xc}, 0x0, ...}, ...)
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/unmarshaler.go:663 +0x234
github.com/zeromicro/go-zero/core/mapping.(*Unmarshaler).processField(0x1400007e3c0?, {{0x102d1d370, 0x5}, {0x0, 0x0}, {0x102f86348, 0x102df7f00}, {0x102d1d376, 0xc}, 0x0, ...}, ...)
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/unmarshaler.go:456 +0x184
github.com/zeromicro/go-zero/core/mapping.(*Unmarshaler).unmarshalWithFullName(0x102e901c0?, {0x102f7c3e0, 0x140005f0080}, {0x102d647e0, 0x140005240c8}, {0x0, 0x0})
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/unmarshaler.go:782 +0x338
github.com/zeromicro/go-zero/core/mapping.(*Unmarshaler).UnmarshalValuer(...)
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/unmarshaler.go:80
github.com/zeromicro/go-zero/core/mapping.(*Unmarshaler).Unmarshal(...)
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/unmarshaler.go:75
github.com/zeromicro/go-zero/core/mapping.unmarshalJsonReader({0x102f78028, 0x14000506378}, {0x102d647e0, 0x140005240c8}, 0x102adb501?)
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/jsonunmarshaler.go:51 +0xc0
github.com/zeromicro/go-zero/core/mapping.UnmarshalJsonReader({0x102f78028, 0x14000506378}, {0x102d647e0, 0x140005240c8}, {0x0?, 0x0?, 0x0?})
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/core/mapping/jsonunmarshaler.go:25 +0x58
github.com/zeromicro/go-zero/rest/httpx.ParseJsonBody(0x14000512400, {0x102d647e0, 0x140005240c8})
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/rest/httpx/requests.go:86 +0xb8
github.com/zeromicro/go-zero/rest/httpx.Parse(0x14000512400, {0x102d647e0, 0x140005240c8})
/Users/qiushye/go/pkg/mod/github.com/zeromicro/[email protected]/rest/httpx/requests.go:42 +0x78
github.com/qbox/jarvis/cmd/jarvis/internal/logic/common/customer.Test_Unmarshal(0x0?)
/Users/qiushye/qbox/jarvis/cmd/jarvis/internal/logic/common/customer/common_test.go:65 +0x244
testing.tRunner(0x140005d8680, 0x102f70020)
/usr/local/go/src/testing/testing.go:1439 +0x110
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1486 +0x300
Environments (please complete the following information):
- OS: darwin/arm64
- go-zero version [1.18]
- goctl version [1.3.4]
More description Add any other context about the problem here.
"19" is not int.
@qiushye please check the fix.
"19"is not int.
"19"is the json format of 19,you can compare the go-zero unmarshal result with json unmarshal result from my case, the latter one works.
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.