Reputation: 41
I have django REST API that I am trying to convert into gRPC. I followed the Django grpc framework guide and created the following files:
class Organization(models.Model):
Org_name = models.CharField(max_length=100, unique=True, primary_key=True, db_index=True)
Address = models.CharField(max_length=100)
Description = models.CharField(max_length=500)
Number_of_emp = models.IntegerField()
package org;
import "google/protobuf/empty.proto";
service OrganizationController {
rpc List(OrganizationListRequest) returns (Organizations) {}
rpc Create(Organization) returns (Organization) {}
rpc Retrieve(OrganizationRetrieveRequest) returns (Organization) {}
rpc Update(Organization) returns (Organization) {}
rpc Destroy(Organization) returns (google.protobuf.Empty) {}
}
message Organization {
string Org_name = 1;
string Address = 2;
string Description = 3;
int32 Number_of_emp = 4;
}
message OrganizationListRequest {
}
message OrganizationRetrieveRequest {
string Org_name = 1;
}
message Organizations {
repeated Organization organization = 1;
}
Note that Organizations is a message declared in org.proto to return a List or an array of objects
class OrganizationService(generics.ModelService):
queryset = Organization.objects.all()
serializer_class = OrganizationSerializerProto
class OrganizationSerializerProto(proto_serializers.ModelProtoSerializer):
class Meta:
model = Organization
proto_class = org_pb2.Organization
fields = '__all__'
Problem I want to make a request using the rpc List(OrganizationListRequest) returns (Organizations) {}
to fetch a list of all the organization in the database. However, whenever I make a call to the rpc, I get the following error:
request error: "error": "13 INTERNAL: Failed to serialize response!" (I use the BloomRPC gui client to make the request)
However, If I change
rpc List(OrganizationListRequest) returns (Organizations) {}
to
rpc List(OrganizationListRequest) returns (stream Organization) {}
the request returns works fine and I get a stream of response of containing allobjects. I do not want to return data in streams, I want it to return an array of objects, but using message Organizations {repeated Organization organization = 1;}
throws the above error.
Am I making a mistake? I searched everywhere but I couldn't find this error. Or is it not possible to make an RPC call like this?
Upvotes: 4
Views: 13677
Reputation: 2091
This is a server-side proto codec error. It usually happen because of:
Organizations
instead of Organization
).gRPC Python will log the exception that is causing the Failed to serialize response!
error. You can enable Python logging explicitly via logging.basicConfig(level=logging.INFO)
. Here is the link to the logging line:
If after above examination, the error still persist, can you create a reproduce case and send to https://github.com/grpc/grpc/issues?
Upvotes: 6