Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc: error unmarshalling request: failed to unmarshal, message is X, want proto.Message #7419

Closed
henriqueleite42 opened this issue Jul 16, 2024 · 1 comment

Comments

@henriqueleite42
Copy link

henriqueleite42 commented Jul 16, 2024

I flowed this tutorial to create a basic grpc server, but I keep receiving this error. I have no idea from where to start to debug it, since it's a generic error. Anyone has any ideas about where to start to validate it?

The files below are simplified, but on the originals all the imports are correct, all the function calls are correct, everything is correct and the code compiles. It never even reaches the AuthenticationController.Google.

Original error

grpc: error unmarshalling request: failed to unmarshal, message is *models.CreateAccountFromExternalProviderInput, want proto.Message

Versions

golang                        v1.22.4
github.com/golang/protobuf    v1.5.4
protoc-gen-go                 v1.34.2
protoc-gen-go-grpc            v1.4.0

Postman

image

authentication.proto

syntax = "proto3";

option go_package = "./internal/delivery/grpc/pb";

service Authentication {
	rpc Google(GoogleInput) returns (AuthOutput);
}

message GoogleInput {
	string Code = 1;
}

message AuthOutput {
	string AccountId = 1;
}

pb.go

package pb

import "github.com/anuntech/authentication/internal/models"

type AuthOutput = models.AuthOutput
type GoogleInput = models.CreateAccountFromExternalProviderInput

controller.go

package grpc

type AuthenticationController struct {
	pb.UnimplementedAuthenticationServer

  authUsecase AuthUsecaseInterface // simplified
}


func (c *AuthenticationController) Google(_ context.Context, in *pb.GoogleInput) (*pb.AuthOutput, error) {
	return c.authUsecase.CreateFromGoogleProvider(in)
}


func AuthenticationController(server *grpc.Server) {
	controller := &AuthenticationController{
		authUsecase: newUsecase(), // simplified
	}

	pb.RegisterAuthenticationServer(server, controller)
}

server.go

package main

func main() {
	server := grpc.NewServer()

	authentication.AuthenticationController(server)

	listener, err := net.Listen("tcp", ":3100")
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
		return
	}

	if err := server.Serve(listener); err != nil {
		log.Fatalf("failed to serve: %v", err)
		return
	}
}

"encoder" (or the thing returned by getCodec)

image
Where proto.Name has the value proto

@henriqueleite42
Copy link
Author

henriqueleite42 commented Jul 16, 2024

I found out that the problem was that I was creating the inputs and outputs manually.

After adding --go_out=. to my protoc command, it created the types correctly.

A huge thanks to @mateothegreat who created this repository and allowed me to see my mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant