Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google.Protobuf dose not allow null for properties

I am using GRPC on top of ASPNETCore, also have a model like this:

syntax = "proto3";
message Blob {
    string id = 1;
    string path = 2;
}

The problem is that when i try to set the path property to null, It throws an ArgumentException.

simply running this code:

new Blob { Path = null };

Results into this:

System.ArgumentNullException: Value cannot be null. (Parameter 'value')
   at Google.Protobuf.ProtoPreconditions.CheckNotNull[T](T value, String name)
   at Grpc.Blob.set_Path(String value)
like image 682
SHM Avatar asked Apr 16 '20 16:04

SHM


1 Answers

Protobuf has no concept of null; with proto3 treating en empty string as "default, do not send", this means there is no payload difference between null and "". So: perhaps don't try and send null?

Alternatively: protobuf-net (which works fine with gRPC via protobuf-net.Grpc, which just attaches to the Google/Microsoft bits) would work fine here, and since it is not tied to proto3, it can be made to treat null and "" differently (sending "" explicitly as a zero length string and not sending null at all). You don't need a .proto for protobuf-net, but if you have one: https://protogen.marcgravell.com/

like image 96
Marc Gravell Avatar answered Sep 25 '22 19:09

Marc Gravell