Parsing of .desc fails for message with custom option

159 views
Skip to first unread message

Marc Schafer

unread,
Feb 9, 2021, 8:39:29 AM2/9/21
to Protocol Buffers
Parsing fails on a .desc file generated from a message with a custom option as shown below. I understand this is happening because the message defined in descriptor.proto doesn't know about my extension but I am not sure how to fix it. I'm using C++.

Here is my .proto:
syntax = "proto3";
package foo;

// extending MessageOptions prevents parsing of generated .desc
import "google/protobuf/descriptor.proto";

// extensions start at 1000
extend google.protobuf.MessageOptions {
  optional string my_option = 1992;
}


message SearchRequest {
  option (my_option) = "custom";  
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

-Best
Marc

Adam Cozzette

unread,
Feb 12, 2021, 2:56:26 PM2/12/21
to Marc Schafer, Protocol Buffers
I think the easiest solution would be to include the .proto file that defines the custom option in the serialized descriptor set. I assume you're generating the .desc file by invoking protoc with -o or --descriptor_set_out, so just make sure to add the custom option .proto file as one of the command line arguments to protoc when you do that.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/614101fd-9571-448b-9b29-0ae050e536cfn%40googlegroups.com.

Marc Schafer

unread,
Feb 12, 2021, 4:37:13 PM2/12/21
to Adam Cozzette, Protocol Buffers

I am invoking protoc –descriptor_set_out on the .proto file with contents described below that include the definition of the custom option. I’m not sure what else you are suggesting I include.

 

I attached the .proto file to clarify the reproduction steps.

protoc –descriptor_set_out simple.proto

 

Use this snippet of code to parse the resulting .desc file:

 

    fdiName = “simple.desc";

    std::ifstream fdi(fdiName);

    google::protobuf::FileDescriptorSet fds;

    auto r = fds.ParseFromIstream(&fdi);

    assert(r);

 

The assertion will fail

 

Best,

Marc

simple.proto

Adam Cozzette

unread,
Feb 12, 2021, 5:05:22 PM2/12/21
to Marc Schafer, Protocol Buffers
I don't think this necessarily has anything to do with the custom option. FileDescriptorSet is itself just a protocol buffer message, so parsing it doesn't involve interpreting the meaning of its contents. It's just like parsing any other proto, so if it's failing then either the descriptors were somehow corrupted or are not being read from disk successfully.

Here is what I would suggest trying:
1. Run this and make sure the results look reasonable: protoc --decode_raw < simple.desc
This will print out the raw field numbers and values in the descriptor.
2. Change your code to read in a string and then parse the string (with ParseFromString instead of ParseFromIstream). This way you can verify that the string you read in exactly matches the file on disk (or at least do a quick check to verify that the length is the same).

Marc Schafer

unread,
Feb 12, 2021, 7:37:45 PM2/12/21
to Adam Cozzette, Protocol Buffers

Thank you for the pointers Adam. The string did not match the file on disk because I neglected to open the file in binary mode. Commenting out the custom option coincidentally made everything work leading me down the rat hole but the real fix is to open in binary mode and then everything works.

Reply all
Reply to author
Forward
0 new messages