Can Nullable types be sent through Protocol Buffers?

In respect that https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/wrappers.proto

You need to import google/protobuf/wrappers.proto in order for this to work.

syntax ="proto3";
package prototest;
import "MessageIdentifier.proto";
import "google/protobuf/wrappers.proto";

message TestMessage {
    string messageTest = 1;
    fixed64 messageTimestampTicks = 2;
    uint32 sequenceNumber = 3;
    MessageUniqueID uniqueID = 4;
    google.protobuf.Int32Value nullableInt = 5; 
}

You can then use it as an int? ,eg nullableInt.HasValue and nullableInt.Value


I will try to improve Nick's answer as it hasn't helped me. grpc compiler claimed that he has no information on google.protobuf.Int32Wrapper type. I have found it is actually called google.protobuf.Int32Value (https://github.com/protocolbuffers/protobuf/blob/48234f5f012582843bb476ee3afef36cda94cb66/src/google/protobuf/wrappers.proto#L88), though google really calls it Int32Wrapper. So the code that helped me was the following:

...
import "google/protobuf/wrappers.proto";
...
message TestMessage {
    ...
    google.protobuf.Int32Value nullableInt = 5; 
}

Other links:

  • C# lib source - https://github.com/protocolbuffers/protobuf/blob/48234f5f012582843bb476ee3afef36cda94cb66/csharp/src/Google.Protobuf/WellKnownTypes/Wrappers.cs#L781
  • C# doc - https://developers.google.com/protocol-buffers/docs/reference/csharp/class/google/protobuf/well-known-types/int32-value