diff --git a/grpcstub_test.go b/grpcstub_test.go index 740308b..64ebe8e 100644 --- a/grpcstub_test.go +++ b/grpcstub_test.go @@ -682,6 +682,27 @@ func TestResponseAny(t *testing.T) { } } +func TestResponseAnyFields(t *testing.T) { + ctx := context.Background() + ts := NewServer(t, "testdata/hello.proto") + t.Cleanup(func() { + ts.Close() + }) + ts.Service("hello.GrpcTestService").Method("HelloFields").ResponseString(`{"field_bytes": "aGVsbG8="}`) // Base64 encoding to pass bytes type + + client := hello.NewGrpcTestServiceClient(ts.Conn()) + res, err := client.HelloFields(ctx, &hello.HelloFieldsRequest{ + FieldBytes: []byte("hello"), + }) + if err != nil { + t.Fatal(err) + } + got := res.FieldBytes + if want := "hello"; string(got) != want { + t.Errorf("got %v\nwant %v", got, want) + } +} + func TestBufProtoRegistry(t *testing.T) { t.Run("Use buf.lock", func(t *testing.T) { ts := NewServer(t, "testdata/bsr/protobuf/pinger/pinger.proto", BufLock("testdata/bsr/protobuf/buf.lock")) diff --git a/testdata/hello.proto b/testdata/hello.proto index b7c2022..2c02c94 100644 --- a/testdata/hello.proto +++ b/testdata/hello.proto @@ -8,6 +8,7 @@ package hello; service GrpcTestService { rpc Hello (HelloRequest) returns (HelloResponse); + rpc HelloFields (HelloFieldsRequest) returns (HelloFieldsResponse); } message HelloRequest { @@ -29,3 +30,11 @@ message HelloResponse { google.protobuf.Timestamp create_time = 5; } + +message HelloFieldsRequest { + bytes field_bytes = 1; +} + +message HelloFieldsResponse { + bytes field_bytes = 1; +} diff --git a/testdata/hello/hello.pb.go b/testdata/hello/hello.pb.go index 1bff53d..19f126a 100644 --- a/testdata/hello/hello.pb.go +++ b/testdata/hello/hello.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.30.0 -// protoc v3.21.12 +// protoc-gen-go v1.33.0 +// protoc v4.25.3 // source: hello.proto package hello @@ -163,6 +163,100 @@ func (x *HelloResponse) GetCreateTime() *timestamppb.Timestamp { return nil } +type HelloFieldsRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + FieldBytes []byte `protobuf:"bytes,1,opt,name=field_bytes,json=fieldBytes,proto3" json:"field_bytes,omitempty"` +} + +func (x *HelloFieldsRequest) Reset() { + *x = HelloFieldsRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_hello_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HelloFieldsRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HelloFieldsRequest) ProtoMessage() {} + +func (x *HelloFieldsRequest) ProtoReflect() protoreflect.Message { + mi := &file_hello_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HelloFieldsRequest.ProtoReflect.Descriptor instead. +func (*HelloFieldsRequest) Descriptor() ([]byte, []int) { + return file_hello_proto_rawDescGZIP(), []int{2} +} + +func (x *HelloFieldsRequest) GetFieldBytes() []byte { + if x != nil { + return x.FieldBytes + } + return nil +} + +type HelloFieldsResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + FieldBytes []byte `protobuf:"bytes,1,opt,name=field_bytes,json=fieldBytes,proto3" json:"field_bytes,omitempty"` +} + +func (x *HelloFieldsResponse) Reset() { + *x = HelloFieldsResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_hello_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HelloFieldsResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HelloFieldsResponse) ProtoMessage() {} + +func (x *HelloFieldsResponse) ProtoReflect() protoreflect.Message { + mi := &file_hello_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HelloFieldsResponse.ProtoReflect.Descriptor instead. +func (*HelloFieldsResponse) Descriptor() ([]byte, []int) { + return file_hello_proto_rawDescGZIP(), []int{3} +} + +func (x *HelloFieldsResponse) GetFieldBytes() []byte { + if x != nil { + return x.FieldBytes + } + return nil +} + var File_hello_proto protoreflect.FileDescriptor var file_hello_proto_rawDesc = []byte{ @@ -188,12 +282,24 @@ var file_hello_proto_rawDesc = []byte{ 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x52, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x42, 0x08, 0x0a, 0x06, 0x5f, 0x77, 0x6f, 0x72, - 0x6c, 0x64, 0x32, 0x45, 0x0a, 0x0f, 0x47, 0x72, 0x70, 0x63, 0x54, 0x65, 0x73, 0x74, 0x53, 0x65, + 0x6c, 0x64, 0x22, 0x35, 0x0a, 0x12, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x46, 0x69, 0x65, 0x6c, 0x64, + 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1f, 0x0a, 0x0b, 0x66, 0x69, 0x65, 0x6c, + 0x64, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0a, 0x66, + 0x69, 0x65, 0x6c, 0x64, 0x42, 0x79, 0x74, 0x65, 0x73, 0x22, 0x36, 0x0a, 0x13, 0x48, 0x65, 0x6c, + 0x6c, 0x6f, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x12, 0x1f, 0x0a, 0x0b, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0a, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x42, 0x79, 0x74, 0x65, + 0x73, 0x32, 0x8b, 0x01, 0x0a, 0x0f, 0x47, 0x72, 0x70, 0x63, 0x54, 0x65, 0x73, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x32, 0x0a, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x12, 0x13, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x48, 0x65, 0x6c, 0x6c, - 0x6f, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, - 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6f, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x44, 0x0a, 0x0b, 0x48, 0x65, 0x6c, + 0x6c, 0x6f, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x19, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, + 0x2e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x48, 0x65, 0x6c, 0x6c, + 0x6f, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, + 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x3b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( @@ -208,19 +314,23 @@ func file_hello_proto_rawDescGZIP() []byte { return file_hello_proto_rawDescData } -var file_hello_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_hello_proto_msgTypes = make([]protoimpl.MessageInfo, 4) var file_hello_proto_goTypes = []interface{}{ (*HelloRequest)(nil), // 0: hello.HelloRequest (*HelloResponse)(nil), // 1: hello.HelloResponse - (*timestamppb.Timestamp)(nil), // 2: google.protobuf.Timestamp + (*HelloFieldsRequest)(nil), // 2: hello.HelloFieldsRequest + (*HelloFieldsResponse)(nil), // 3: hello.HelloFieldsResponse + (*timestamppb.Timestamp)(nil), // 4: google.protobuf.Timestamp } var file_hello_proto_depIdxs = []int32{ - 2, // 0: hello.HelloRequest.request_time:type_name -> google.protobuf.Timestamp - 2, // 1: hello.HelloResponse.create_time:type_name -> google.protobuf.Timestamp + 4, // 0: hello.HelloRequest.request_time:type_name -> google.protobuf.Timestamp + 4, // 1: hello.HelloResponse.create_time:type_name -> google.protobuf.Timestamp 0, // 2: hello.GrpcTestService.Hello:input_type -> hello.HelloRequest - 1, // 3: hello.GrpcTestService.Hello:output_type -> hello.HelloResponse - 3, // [3:4] is the sub-list for method output_type - 2, // [2:3] is the sub-list for method input_type + 2, // 3: hello.GrpcTestService.HelloFields:input_type -> hello.HelloFieldsRequest + 1, // 4: hello.GrpcTestService.Hello:output_type -> hello.HelloResponse + 3, // 5: hello.GrpcTestService.HelloFields:output_type -> hello.HelloFieldsResponse + 4, // [4:6] is the sub-list for method output_type + 2, // [2:4] is the sub-list for method input_type 2, // [2:2] is the sub-list for extension type_name 2, // [2:2] is the sub-list for extension extendee 0, // [0:2] is the sub-list for field type_name @@ -256,6 +366,30 @@ func file_hello_proto_init() { return nil } } + file_hello_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HelloFieldsRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_hello_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HelloFieldsResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } file_hello_proto_msgTypes[1].OneofWrappers = []interface{}{} type x struct{} @@ -264,7 +398,7 @@ func file_hello_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_hello_proto_rawDesc, NumEnums: 0, - NumMessages: 2, + NumMessages: 4, NumExtensions: 0, NumServices: 1, }, diff --git a/testdata/hello/hello_grpc.pb.go b/testdata/hello/hello_grpc.pb.go index a693966..ad0b388 100644 --- a/testdata/hello/hello_grpc.pb.go +++ b/testdata/hello/hello_grpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.3.0 -// - protoc v3.21.12 +// - protoc v4.25.3 // source: hello.proto package hello @@ -19,7 +19,8 @@ import ( const _ = grpc.SupportPackageIsVersion7 const ( - GrpcTestService_Hello_FullMethodName = "/hello.GrpcTestService/Hello" + GrpcTestService_Hello_FullMethodName = "/hello.GrpcTestService/Hello" + GrpcTestService_HelloFields_FullMethodName = "/hello.GrpcTestService/HelloFields" ) // GrpcTestServiceClient is the client API for GrpcTestService service. @@ -27,6 +28,7 @@ const ( // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type GrpcTestServiceClient interface { Hello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloResponse, error) + HelloFields(ctx context.Context, in *HelloFieldsRequest, opts ...grpc.CallOption) (*HelloFieldsResponse, error) } type grpcTestServiceClient struct { @@ -46,11 +48,21 @@ func (c *grpcTestServiceClient) Hello(ctx context.Context, in *HelloRequest, opt return out, nil } +func (c *grpcTestServiceClient) HelloFields(ctx context.Context, in *HelloFieldsRequest, opts ...grpc.CallOption) (*HelloFieldsResponse, error) { + out := new(HelloFieldsResponse) + err := c.cc.Invoke(ctx, GrpcTestService_HelloFields_FullMethodName, in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // GrpcTestServiceServer is the server API for GrpcTestService service. // All implementations must embed UnimplementedGrpcTestServiceServer // for forward compatibility type GrpcTestServiceServer interface { Hello(context.Context, *HelloRequest) (*HelloResponse, error) + HelloFields(context.Context, *HelloFieldsRequest) (*HelloFieldsResponse, error) mustEmbedUnimplementedGrpcTestServiceServer() } @@ -61,6 +73,9 @@ type UnimplementedGrpcTestServiceServer struct { func (UnimplementedGrpcTestServiceServer) Hello(context.Context, *HelloRequest) (*HelloResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method Hello not implemented") } +func (UnimplementedGrpcTestServiceServer) HelloFields(context.Context, *HelloFieldsRequest) (*HelloFieldsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method HelloFields not implemented") +} func (UnimplementedGrpcTestServiceServer) mustEmbedUnimplementedGrpcTestServiceServer() {} // UnsafeGrpcTestServiceServer may be embedded to opt out of forward compatibility for this service. @@ -92,6 +107,24 @@ func _GrpcTestService_Hello_Handler(srv interface{}, ctx context.Context, dec fu return interceptor(ctx, in, info, handler) } +func _GrpcTestService_HelloFields_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(HelloFieldsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GrpcTestServiceServer).HelloFields(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: GrpcTestService_HelloFields_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GrpcTestServiceServer).HelloFields(ctx, req.(*HelloFieldsRequest)) + } + return interceptor(ctx, in, info, handler) +} + // GrpcTestService_ServiceDesc is the grpc.ServiceDesc for GrpcTestService service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -103,6 +136,10 @@ var GrpcTestService_ServiceDesc = grpc.ServiceDesc{ MethodName: "Hello", Handler: _GrpcTestService_Hello_Handler, }, + { + MethodName: "HelloFields", + Handler: _GrpcTestService_HelloFields_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "hello.proto", diff --git a/testdata/routeguide/route_guide.pb.go b/testdata/routeguide/route_guide.pb.go index 5e2d70a..4eb5138 100644 --- a/testdata/routeguide/route_guide.pb.go +++ b/testdata/routeguide/route_guide.pb.go @@ -14,8 +14,8 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.30.0 -// protoc v3.21.12 +// protoc-gen-go v1.33.0 +// protoc v4.25.3 // source: route_guide.proto package routeguide diff --git a/testdata/routeguide/route_guide_grpc.pb.go b/testdata/routeguide/route_guide_grpc.pb.go index 00b8007..d530eba 100644 --- a/testdata/routeguide/route_guide_grpc.pb.go +++ b/testdata/routeguide/route_guide_grpc.pb.go @@ -15,7 +15,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.3.0 -// - protoc v3.21.12 +// - protoc v4.25.3 // source: route_guide.proto package routeguide