Skip to content

Commit

Permalink
jsonpb: avoid unexported fields in hand-crafted message (#671)
Browse files Browse the repository at this point in the history
The purego implementation is unable to handle unexported fields since it is
impossible for purego code to mutate such fields without using unsafe.

Also, modify the Makefile to test the purego code paths.

Fixes #670
  • Loading branch information
dsnet committed Aug 3, 2018
1 parent f5983d5 commit 7d1b268
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ install:

test:
go test ./... ./protoc-gen-go/testdata
go test -tags purego ./... ./protoc-gen-go/testdata
go build ./protoc-gen-go/testdata/grpc/grpc.pb.go
make -C conformance test

Expand Down
24 changes: 12 additions & 12 deletions jsonpb/jsonpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func TestMarshalIllegalTime(t *testing.T) {

func TestMarshalJSONPBMarshaler(t *testing.T) {
rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
msg := dynamicMessage{rawJson: rawJson}
msg := dynamicMessage{RawJson: rawJson}
str, err := new(Marshaler).MarshalToString(&msg)
if err != nil {
t.Errorf("an unexpected error occurred when marshalling JSONPBMarshaler: %v", err)
Expand All @@ -583,7 +583,7 @@ func TestMarshalJSONPBMarshaler(t *testing.T) {
}

func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
msg := dynamicMessage{RawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
a, err := ptypes.MarshalAny(&msg)
if err != nil {
t.Errorf("an unexpected error occurred when marshalling to Any: %v", err)
Expand All @@ -601,7 +601,7 @@ func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
}

func TestMarshalWithCustomValidation(t *testing.T) {
msg := dynamicMessage{rawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`, dummy: &dynamicMessage{}}
msg := dynamicMessage{RawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`, Dummy: &dynamicMessage{}}

js, err := new(Marshaler).MarshalToString(&msg)
if err != nil {
Expand Down Expand Up @@ -1011,8 +1011,8 @@ func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
if err := Unmarshal(strings.NewReader(rawJson), &msg); err != nil {
t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
}
if msg.rawJson != rawJson {
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.rawJson, rawJson)
if msg.RawJson != rawJson {
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.RawJson, rawJson)
}
}

Expand All @@ -1036,7 +1036,7 @@ func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) {
t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
}

dm := &dynamicMessage{rawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
dm := &dynamicMessage{RawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
var want anypb.Any
if b, err := proto.Marshal(dm); err != nil {
t.Errorf("an unexpected error occurred when marshaling message: %v", err)
Expand Down Expand Up @@ -1097,30 +1097,30 @@ func (s *stringField) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
// dynamicMessage implements protobuf.Message but is not a normal generated message type.
// It provides implementations of JSONPBMarshaler and JSONPBUnmarshaler for JSON support.
type dynamicMessage struct {
rawJson string `protobuf:"bytes,1,opt,name=rawJson"`
RawJson string `protobuf:"bytes,1,opt,name=rawJson"`

// an unexported nested message is present just to ensure that it
// won't result in a panic (see issue #509)
dummy *dynamicMessage `protobuf:"bytes,2,opt,name=dummy"`
Dummy *dynamicMessage `protobuf:"bytes,2,opt,name=dummy"`
}

func (m *dynamicMessage) Reset() {
m.rawJson = "{}"
m.RawJson = "{}"
}

func (m *dynamicMessage) String() string {
return m.rawJson
return m.RawJson
}

func (m *dynamicMessage) ProtoMessage() {
}

func (m *dynamicMessage) MarshalJSONPB(jm *Marshaler) ([]byte, error) {
return []byte(m.rawJson), nil
return []byte(m.RawJson), nil
}

func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
m.rawJson = string(js)
m.RawJson = string(js)
return nil
}

Expand Down

0 comments on commit 7d1b268

Please sign in to comment.