mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
This commit is contained in:
@ -15,6 +15,7 @@ import (
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/guid"
|
||||
@ -419,3 +420,68 @@ func Test_Router_Handler_Strict_ParameterCaseSensitive(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type testJsonRawMessageIssue3449Req struct {
|
||||
g.Meta `path:"/test" method:"POST" sm:"hello" tags:"示例"`
|
||||
|
||||
Name string `json:"name" v:"required" dc:"名称"`
|
||||
JSONRaw json.RawMessage `json:"jsonRaw" dc:"原始JSON"`
|
||||
}
|
||||
type testJsonRawMessageIssue3449Res struct {
|
||||
Name string `json:"name" v:"required" dc:"名称"`
|
||||
JSONRaw json.RawMessage `json:"jsonRaw" dc:"原始JSON"`
|
||||
}
|
||||
|
||||
type testJsonRawMessageIssue3449 struct {
|
||||
}
|
||||
|
||||
func (t *testJsonRawMessageIssue3449) Test(ctx context.Context, req *testJsonRawMessageIssue3449Req) (res *testJsonRawMessageIssue3449Res, err error) {
|
||||
return &testJsonRawMessageIssue3449Res{
|
||||
Name: req.Name,
|
||||
JSONRaw: req.JSONRaw,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/3449
|
||||
func Test_JsonRawMessage_Issue3449(t *testing.T) {
|
||||
|
||||
s := g.Server(guid.S())
|
||||
s.Use(ghttp.MiddlewareHandlerResponse)
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Bind(new(testJsonRawMessageIssue3449))
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
v1 := map[string]any{
|
||||
"jkey1": "11",
|
||||
"jkey2": "12",
|
||||
}
|
||||
|
||||
v2 := map[string]any{
|
||||
"jkey1": "21",
|
||||
"jkey2": "22",
|
||||
}
|
||||
data := map[string]any{
|
||||
"Name": "test",
|
||||
"jsonRaw": []any{
|
||||
v1, v2,
|
||||
},
|
||||
}
|
||||
|
||||
expect1 := `{"code":0,"message":"","data":{"name":"test","jsonRaw":[{"jkey1":"11","jkey2":"12"},{"jkey1":"21","jkey2":"22"}]}}`
|
||||
t.Assert(client.PostContent(ctx, "/test", data), expect1)
|
||||
|
||||
expect2 := `{"code":0,"message":"","data":{"name":"test","jsonRaw":{"jkey1":"11","jkey2":"12"}}}`
|
||||
t.Assert(client.PostContent(ctx, "/test", map[string]any{
|
||||
"Name": "test",
|
||||
"jsonRaw": v1,
|
||||
}), expect2)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -7,9 +7,12 @@
|
||||
package gconv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/internal/intlog"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
@ -270,7 +273,12 @@ func doConvert(in doConvertInput) (convertedValue interface{}) {
|
||||
return Maps(in.FromValue)
|
||||
|
||||
case "RawMessage", "json.RawMessage":
|
||||
return Bytes(in.FromValue)
|
||||
// issue 3449
|
||||
bytes, err := json.Marshal(in.FromValue)
|
||||
if err != nil {
|
||||
intlog.Errorf(context.TODO(), `%+v`, err)
|
||||
}
|
||||
return bytes
|
||||
|
||||
default:
|
||||
if in.ReferValue != nil {
|
||||
|
||||
@ -1303,6 +1303,33 @@ func Test_Struct_Issue1597(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/3449
|
||||
func Test_Struct_Issue3449(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type S struct {
|
||||
A int
|
||||
B json.RawMessage
|
||||
}
|
||||
|
||||
jsonByte := []byte(`{
|
||||
"a":1,
|
||||
"b":[{
|
||||
"k1": "11",
|
||||
"k2": "12"
|
||||
},
|
||||
{
|
||||
"k1": "21",
|
||||
"k2": "22"
|
||||
}]}`)
|
||||
data, err := gjson.DecodeToJson(jsonByte)
|
||||
t.AssertNil(err)
|
||||
s := &S{}
|
||||
err = data.Scan(s)
|
||||
t.AssertNil(err)
|
||||
t.Assert(s.B, `[{"k1":"11","k2":"12"},{"k1":"21","k2":"22"}]`)
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/2980
|
||||
func Test_Struct_Issue2980(t *testing.T) {
|
||||
type Post struct {
|
||||
|
||||
Reference in New Issue
Block a user