Skip to content

Commit

Permalink
fix:(thrift) api.body shoudn't change field alias by default (for j…
Browse files Browse the repository at this point in the history
…son-generic)
  • Loading branch information
AsterDY committed Jan 7, 2025
1 parent 55704ea commit 74bc9f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
66 changes: 44 additions & 22 deletions conv/j2t/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,29 +552,51 @@ func TestNullJSON2Thrift(t *testing.T) {
}

func TestApiBody(t *testing.T) {
desc := getExampleDescByName("ApiBodyMethod", true, thrift.Options{})
data := []byte(`{"code":1024,"InnerCode":{}}`)
cv := NewBinaryConv(conv.Options{
EnableHttpMapping: true,
WriteDefaultField: true,
ReadHttpValueFallback: true,
TracebackRequredOrRootFields: true,
t.Run("http", func(t *testing.T) {
desc := getExampleDescByName("ApiBodyMethod", true, thrift.Options{
ApiBodyFastPath: true,
})
data := []byte(`{"code":1024,"Code":2048,"InnerCode":{}}`)
cv := NewBinaryConv(conv.Options{
EnableHttpMapping: true,
WriteDefaultField: true,
ReadHttpValueFallback: true,
TracebackRequredOrRootFields: true,
})
ctx := context.Background()
req, err := stdh.NewRequest("POST", "http://localhost:8888/root", bytes.NewBuffer(data))
require.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
r, _ := http.NewHTTPRequestFromStdReq(req)
ctx = context.WithValue(ctx, conv.CtxKeyHTTPRequest, r)
out, err := cv.Do(ctx, desc, data)
require.Nil(t, err)
act := example3.NewExampleApiBody()
_, err = act.FastRead(out)
require.Nil(t, err)
require.Equal(t, int64(1024), act.Code)
require.Equal(t, int16(1024), act.Code2)
require.Equal(t, int64(1024), act.InnerCode.C1)
require.Equal(t, int16(0), act.InnerCode.C2)
})
t.Run("not http", func(t *testing.T) {
desc := getExampleDescByName("ApiBodyMethod", true, thrift.Options{
ApiBodyFastPath: false,
})
data := []byte(`{"code":1024,"Code":2048,"InnerCode":{"C1":1,"code":2}}`)
cv := NewBinaryConv(conv.Options{
WriteDefaultField: true,
})
out, err := cv.Do(context.Background(), desc, data)
require.Nil(t, err)
act := example3.NewExampleApiBody()
_, err = act.FastRead(out)
require.Nil(t, err)
require.Equal(t, int64(2048), act.Code)
require.Equal(t, int16(1024), act.Code2)
require.Equal(t, int64(1), act.InnerCode.C1)
require.Equal(t, int16(2), act.InnerCode.C2)
})
ctx := context.Background()
req, err := stdh.NewRequest("POST", "http://localhost:8888/root", bytes.NewBuffer(data))
require.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
r, err := http.NewHTTPRequestFromStdReq(req)
ctx = context.WithValue(ctx, conv.CtxKeyHTTPRequest, r)
out, err := cv.Do(ctx, desc, data)
require.Nil(t, err)
act := example3.NewExampleApiBody()
_, err = act.FastRead(out)
require.Nil(t, err)
require.Equal(t, int64(1024), act.Code)
require.Equal(t, int16(1024), act.Code2)
require.Equal(t, int64(1024), act.InnerCode.C1)
require.Equal(t, int16(0), act.InnerCode.C2)
}

func TestError(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions thrift/annotation/anno_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func (m apiBodyMapper) Map(ctx context.Context, anns []parser.Annotation, desc i
if len(ann.Values) != 1 {
return nil, nil, errors.New("api.body must have a value")
}
ret = append(ret, parser.Annotation{
Key: APIKeyName,
Values: []string{ann.Values[0]},
})
isRoot := ctx.Value(thrift.CtxKeyIsBodyRoot)
// special fast-path: if the field is at body root, we don't need to add api.body
if isRoot != nil && isRoot.(bool) {
// special fast-path: if the field is at body root, we don't need to add api.body to call GetMapBody()
if opts.ApiBodyFastPath && isRoot != nil && isRoot.(bool) {
ret = append(ret, parser.Annotation{
Key: APIKeyName,
Values: []string{ann.Values[0]},
})
continue
} else {
ret = append(ret, parser.Annotation{
Expand Down
3 changes: 3 additions & 0 deletions thrift/idl.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ type Options struct {
// `// path := /a/b/c.thrift` will got ["/a/b/c.thrift"]
// NOTICE: at present, only StructDescriptor.Annotations() can get this
PutThriftFilenameToAnnotation bool

// ApiBodyFastPath indicates `api.body` will change alias-name of root field, which can avoid search http-body on them
ApiBodyFastPath bool
}

// NewDefaultOptions creates a default Options.
Expand Down

0 comments on commit 74bc9f2

Please sign in to comment.