Search code examples
protocol-buffersprotobuf-cproto3

Defined required fields in proto3


I am using protobuf with version3 in Php. Below is my proto file

syntax ="proto3";

package message.events.user.v1;

message UserWasActivatedMessage {
  int32 userId = 1 ;
  string eventType = 2;
}

This is my proto file and whenever I do not set any user or event proto is automatically setting them up with a default value which I do not want, now I want user to explicitly define every value whenever they send a message as this will reduce the chance of not defining any value


Solution

  • This is a "feature" of protocol buffers and cannot be overriden using the standard SDKs. See:

    https://developers.google.com/protocol-buffers/docs/proto3#default

    I assume the requirement of having default values is a result of the schemaless nature of on-the-wire messages, where all fields must be included and there being no way to specify a value as nil.

    Yes, there's a difference in meaning between nil and default but you can't reflect that in protocol buffers.