Skip to content

defining a customtype as a fake proto message causes problems with jsonpb, prototext and possibly some others #132

Open
@dennwc

Description

Can be reproduced with custom type:

package pt

//go:generate protoc --proto_path=$GOPATH/src:. --gogo_out=. types.proto

type Native struct{
    data string
}

// ... omiting methods to satisfy protobuf interfaces.
// types.proto
syntax = "proto3";

package pt;

// Contains custom type definition to bypass generation errors:
// message Native{}
import ".../native.proto";

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

option (gogoproto.sizer_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.unmarshaler_all) =  true;
option (gogoproto.testgen_all) =  true;
option (gogoproto.populate_all) =  true;
option (gogoproto.equal_all) =  true;

message Item{
    Native sub = 1;
}

Auto-generated tests for serialization will fail with panic:

panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

reflect.valueInterface(...)
    /usr/local/go/src/reflect/value.go:912 +0xe7
reflect.Value.Interface(...)
    /usr/local/go/src/reflect/value.go:901
github.com/gogo/protobuf/jsonpb.(*Marshaler).marshalValue(...)
    src/github.com/gogo/protobuf/jsonpb/jsonpb.go:371
github.com/gogo/protobuf/jsonpb.(*Marshaler).marshalField(...)
    src/github.com/gogo/protobuf/jsonpb/jsonpb.go:216
github.com/gogo/protobuf/jsonpb.(*Marshaler).marshalObject(...)
    src/github.com/gogo/protobuf/jsonpb/jsonpb.go:147
github.com/gogo/protobuf/jsonpb.(*Marshaler).marshalValue(...)
    src/github.com/gogo/protobuf/jsonpb/jsonpb.go:316
github.com/gogo/protobuf/jsonpb.(*Marshaler).marshalField(...)
    src/github.com/gogo/protobuf/jsonpb/jsonpb.go:216
github.com/gogo/protobuf/jsonpb.(*Marshaler).marshalObject(...)
    src/github.com/gogo/protobuf/jsonpb/jsonpb.go:147
github.com/gogo/protobuf/jsonpb.(*Marshaler).Marshal(...)
    src/github.com/gogo/protobuf/jsonpb/jsonpb.go:75
github.com/gogo/protobuf/jsonpb.(*Marshaler).MarshalToString(...)
    src/github.com/gogo/protobuf/jsonpb/jsonpb.go:81

Jsonpb definitely has no code to test for unexported fields.

But this can be fixed easier: jsonpb code should check if object implements json.Marshaler.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions