2

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";

Nock
  • 6,561
  • 1
  • 28
  • 27

2 Answers2

2

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Concerning the comment about descriptor.proto, it means I shouldn't be concerned by the warning message? I've thoroughly check the tool's usage but there's nothing related to this specific option, only comment about "+OPTION=VALUE Specify a custom OPTION/VALUE pair" I don't know which option should be selected and how... – Nock May 24 '18 at 21:17
  • I've cloned the Git repo and took a look at `src\protobuf-net.Reflection\CSharpCodeGenerator.cs` and I don't see in the generated code anything that would allow me to check if a given field was transported. So I'll ask differently because I may misunderstanding something: How do I check from an instance of a Protobuf object if a given field that was optional was serialized or not? – Nock May 24 '18 at 21:57
  • @Nock it looks like `ShouldSerialize*()` is always emitted; does that give you what you need? (`ShouldSerialize*()` and `*Specified` are basically the same thing) – Marc Gravell May 24 '18 at 23:17
  • I thought that ShouldSerialize was more for the creation of the Protobuf data, isn't? My use case is I create a Protobuf object from a data stream and I want to know if a given optional properties was part of the data stream or not. It's for when I get a protobuf data from the network and I want to create my model object out of the deserialized data. – Nock May 25 '18 at 08:23
  • @Nock for the generated types, the two concepts are interchangeable, since the idea is that you should be able to "round trip" and it should serialize pretty much the same as the original data. Thus: `ShouldSerialize*` returning true should happen if and only if there was data loaded for that member. Do you have an example where this isn't behaving in this way? – Marc Gravell May 25 '18 at 09:07
  • Oh ok, I wasn't understand ShouldSerialize this way, thank you for clarification and all the help! – Nock May 25 '18 at 11:02
0

It is a legacy issue. I have to parse google Protobuf 2 messages in .NET. So, Marc, please clarify if your current protobuf-net version (and future versions will) support Protobuf 2 has_() method. If so, can protogen generate has_() method from proto file? It it can, how to do it with protogen?

  • 2
    This does not provide an answer to the question. You can [search for similar questions](//stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](//stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour) – Yunnosch Jul 23 '20 at 10:56
  • The link is this: https://stackoverflow.com/questions/18889249/protobuf-net-missing-has-function-for-optional-fields/18910990#18910990. But it does not clarify the current protobuf-net version state related to my question. The current protogen version does not offer a switch -p:detectMissing switch to generate has_*() functions. – Paul Tomov Jul 23 '20 at 11:13
  • @PaulTomov protobuf-net doesn't generate `has_` methods - it generates `ShouldSerialize*` methods when needed, where "when needed" means "for optional members"; if you're using `syntax = "proto3"`, then presence tracking is disabled by default intentionally, but the (currently experimental) new `optional` feature in proto3 is supported (it turns presence tracking back on at the per-member level). This works by default - there's no need to add the `--experimental_allow_proto3_optional` flag that `protoc` requires to enable this feature – Marc Gravell Jul 24 '20 at 11:34
  • Thank you Marc. I verified that ShouldSerialize*() method of your protobuf-net library represents (is equivalent) of Has_*() method of protobuf 2. Please continue to keep the compatibility of your protobuf-net library with google protobuf 2 specification. A lot of developers prefer and will prefer Protobuf 2 over Protobuf 3. – Paul Tomov Jul 26 '20 at 21:22