Skip to content

Commit

Permalink
Fixed []byte unmarshaling for non proto structs (grpc-ecosystem#2693)
Browse files Browse the repository at this point in the history
  • Loading branch information
gknw authored May 19, 2022
1 parent 83bd273 commit 3eca6bd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/marshal_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ var (
{data: "", json: `""`},
{data: proto.String(""), json: `""`},
{data: "foo", json: `"foo"`},
{data: []byte("foo"), json: `"Zm9v"`},
{data: []byte{}, json: `""`},
{data: proto.String("foo"), json: `"foo"`},
{data: int32(-1), json: "-1"},
{data: proto.Int32(-1), json: "-1"},
Expand Down
11 changes: 11 additions & 0 deletions runtime/marshal_jsonpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ func decodeNonProtoField(d *json.Decoder, unmarshaler protojson.UnmarshalOptions
return nil
}
if rv.Kind() == reflect.Slice {
if rv.Type().Elem().Kind() == reflect.Uint8 {
var sl []byte
if err := d.Decode(&sl); err != nil {
return err
}
if sl != nil {
rv.SetBytes(sl)
}
return nil
}

var sl []json.RawMessage
if err := d.Decode(&sl); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions runtime/marshal_jsonpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ var (
{data: uint64(1), json: "1"},
{data: proto.Uint64(1), json: "1"},
{data: "abc", json: `"abc"`},
{data: []byte("abc"), json: `"YWJj"`},
{data: []byte{}, json: `""`},
{data: proto.String("abc"), json: `"abc"`},
{data: float32(1.5), json: "1.5"},
{data: proto.Float32(1.5), json: "1.5"},
Expand Down

0 comments on commit 3eca6bd

Please sign in to comment.