Search code examples
protobuf-netprotogen

Can generate optional fields correctly with protogen.exe


I've downloaded the CLI protogen.exe from here https://protogen.marcgravell.com/ and I try to generate a C# file with *Specified accessors for optional members, as stated here using this command line:

protogen --csharp_out="." MyProtoFile.proto +p:detectMissing

All optional members using a value type don't have either nullable type or a Specified accessor.

I also constantly get this warning message:

google/protobuf/descriptor.proto(41,1,41,8): warning: no syntax specified; it is strongly recommended to specify 'syntax="proto2";' or 'syntax="proto3";'

And I don't understand because the first line of my proto file is:

syntax="proto2";


Solution

  • The warning is coming from descriptor.proto, not your proto. I will consider whether this should be suppressed for imported files.

    The "detect missing" issue is because that previous answer is outdated and relates to the r668 version of protogen. The protogen tool has been completely rewritten as fully managed code in the last year, and presumably that feature either no longer applies or is implemented differently. I'm not at a PC to check, but: that's the fundamental cause. If you run the tool without any options it should show you the supported usage.

    Edit: ShouldSerialize() methods are provided when appropriate, and should work for this purpose.