gogo / protobuf

[Deprecated] Protocol Buffers for Go with Gadgets
Other
5.67k stars 808 forks source link

Panic: invalid Go type #750

Open ivch opened 2 years ago

ivch commented 2 years ago

I'm using the following versions of libraries:

github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.2
google.golang.org/protobuf v1.27.1

libprotoc 3.19.4

Here is my test proto message:

syntax = "proto3";
package json;

message TestMessage {
    double float64 = 1 [(gogoproto.nullable) = false, (gogoproto.customtype) = "Float64"];
    bytes obj = 2      [(gogoproto.nullable) = false, (gogoproto.customtype) = "Object"];
}

Here is my code:

func TestObjectMarshallUnmarshallProtobuf(t *testing.T) {
    _, err := proto.Marshal(&TestMessage{
        Obj: Object{
            Object: struct {
                Hello string `json:"hello"`
            }{
                Hello: "world",
            },
        },
    })
    if err != nil {
        t.Fatal(err)
    }
}

And as a result getting this panic message:

panic: invalid Go type json.Object for field json.TestMessage.obj [recovered]
    panic: invalid Go type json.Object for field json.TestMessage.obj

goroutine 4 [running]:
testing.tRunner.func1.2({0x1279880, 0xc000056930})
    /usr/local/go/src/testing/testing.go:1209 +0x24e
testing.tRunner.func1()
    /usr/local/go/src/testing/testing.go:1212 +0x218
panic({0x1279880, 0xc000056930})
    /usr/local/go/src/runtime/panic.go:1038 +0x215
google.golang.org/protobuf/internal/impl.newSingularConverter({0x1336550, 0x129de80}, {0x1336be8, 0xc00000a2d0})
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/internal/impl/convert.go:143 +0xb9c
google.golang.org/protobuf/internal/impl.NewConverter({0x1336550, 0x129de80}, {0x1336be8, 0xc00000a2d0})
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/internal/impl/convert.go:60 +0xa5
google.golang.org/protobuf/internal/impl.fieldInfoForScalar({0x1336be8, 0xc00000a2d0}, {{0x1282c6e, 0x3}, {0x0, 0x0}, {0x1336550, 0x129de80}, {0x1282c72, 0x43}, ...}, ...)
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/internal/impl/message_reflect_field.go:270 +0x190
google.golang.org/protobuf/internal/impl.(*MessageInfo).makeKnownFieldsFunc(0xc000128420, {0x48, {0x1336550, 0x1279180}, 0xffffffffffffffff, {0x0, 0x0}, 0x30, {0x1336550, 0x1274460}, ...})
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/internal/impl/message_reflect.go:80 +0x7c8
google.golang.org/protobuf/internal/impl.(*MessageInfo).makeReflectFuncs(0xc000128420, {0x1336550, 0x12b4500}, {0x48, {0x1336550, 0x1279180}, 0xffffffffffffffff, {0x0, 0x0}, 0x30, ...})
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/internal/impl/message_reflect.go:42 +0x65
google.golang.org/protobuf/internal/impl.(*MessageInfo).initOnce(0xc000128420)
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/internal/impl/message.go:91 +0x1d0
google.golang.org/protobuf/internal/impl.(*MessageInfo).init(...)
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/internal/impl/message.go:73
google.golang.org/protobuf/internal/impl.(*messageReflectWrapper).ProtoMethods(0xc000056880)
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/internal/impl/message_reflect_gen.go:150 +0x31
google.golang.org/protobuf/proto.protoMethods(...)
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/proto/proto_methods.go:18
google.golang.org/protobuf/proto.MarshalOptions.marshal({{}, 0x78, 0x0, 0x0}, {0x0, 0x12bc5a0, 0xc000010280}, {0x1335818, 0xc000056880})
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/proto/encode.go:140 +0x63
google.golang.org/protobuf/proto.MarshalOptions.MarshalAppend({{}, 0xa0, 0xc5, 0x2b}, {0x0, 0x0, 0x0}, {0x1329660, 0xc000056880})
    /Users/ivch/workspace/go/pkg/mod/google.golang.org/protobuf@v1.27.1/proto/encode.go:122 +0x79
github.com/golang/protobuf/proto.marshalAppend({0x0, 0x0, 0x0}, {0x132e820, 0xc000010280}, 0x71)
    /Users/ivch/workspace/go/pkg/mod/github.com/golang/protobuf@v1.5.2/proto/wire.go:40 +0xa5
github.com/golang/protobuf/proto.Marshal(...)
    /Users/ivch/workspace/go/pkg/mod/github.com/golang/protobuf@v1.5.2/proto/wire.go:23
swarm/tools/json.TestObjectMarshallUnmarshallProtobuf(0xc00016c1a0)
    /Users/ivch/workspace/go/src/github.com/{path}/object_test.go:85 +0x93
testing.tRunner(0xc00016c1a0, 0x12e45b8)
    /usr/local/go/src/testing/testing.go:1259 +0x102
created by testing.(*T).Run
    /usr/local/go/src/testing/testing.go:1306 +0x35a
abaruchi commented 2 years ago

Hi, I'm having a similar issue when using the nullable option. I'd like to know if you found some fix to the problem?

message SomeMessageRequest {
    repeated ServiceField request = 1 [(gogoproto.nullable) = false];
}

I noticed that removing the nullabe kind of fix it, but I'd like to use the nullable option. So, as far as I can tell.. looks like there is a bug when using this option.

Thanks.

Elojah commented 2 years ago

Hey @ivch , by luck have you found out a solution to this issue ?

jeschkies commented 1 year ago

I came across the same issue even without nullable. It does work somewhere else.