Skip to content

Commit

Permalink
fix: not support unknown-fields for ReadAnyWithDesc()
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Aug 21, 2023
1 parent 1942a99 commit 00ca1e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
8 changes: 5 additions & 3 deletions thrift/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,9 +1137,12 @@ func (p *BinaryProtocol) ReadAnyWithDesc(desc *TypeDescriptor, byteAsUint8 bool,
}
next := st.FieldById(id)
if next == nil {
if !disallowUnknonw {
if disallowUnknonw {
return nil, errUnknonwField
}
if err := p.Skip(typ, false); err != nil {
return nil, err
}
continue
}
vv, err := p.ReadAnyWithDesc(next.Type(), byteAsUint8, copyString, disallowUnknonw, useFieldName)
Expand Down Expand Up @@ -1592,7 +1595,6 @@ func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{},
return e
}
}
return nil
} else {
vs, ok := val.(map[FieldID]interface{})
if !ok {
Expand Down Expand Up @@ -1627,8 +1629,8 @@ func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{},
// return e
// }
// FreeRequiresBitmap(r)
return nil
}
return p.WriteStructEnd()
default:
return errUnsupportedType
}
Expand Down
33 changes: 32 additions & 1 deletion thrift/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package thrift

import (
"context"
"fmt"
"io/ioutil"
"os"
"runtime"
Expand Down Expand Up @@ -59,4 +60,34 @@ func getExampleData() []byte {
panic(err)
}
return out
}
}

func TestBinaryProtocol_ReadAnyWithDesc(t *testing.T) {
p1, err := NewDescritorFromPath(context.Background(), "../testdata/idl/example3.thrift")
if err != nil {
panic(err)
}
exp3partial := p1.Functions()["PartialMethod"].Response().Struct().FieldById(0).Type()
data, err := ioutil.ReadFile("../testdata/data/example3.bin")
if err != nil {
panic(err)
}

p := NewBinaryProtocol(data)
v, err := p.ReadAnyWithDesc(exp3partial, false, false, false, true)
if err != nil {
panic(err)
}
fmt.Printf("%#v", v)
p = NewBinaryProtocolBuffer()
err = p.WriteAnyWithDesc(exp3partial, v, true, true, true)
if err != nil {
panic(err)
}
fmt.Printf("%x", p.RawBuf())
v, err = p.ReadAnyWithDesc(exp3partial, false, false, false, true)
if err != nil {
panic(err)
}
fmt.Printf("%#v", v)
}

0 comments on commit 00ca1e0

Please sign in to comment.