I have exteded data in my .proto file:

option java_outer_classname = "Foobar";
message Foo {
    optional int32 i = 1;
    extensions 10 to 99999;
message Bar {
    extend Foo {
        optional int32 j = 10001;
        optional string name = 10002;
message Msg {
    optional Foo foo = 1;

So, here is code which i'm using to parse data using DynamicMessage:
// Create data
        Foobar.Foo foo = Foobar.Foo.newBuilder()
                .setExtension(Foobar.Bar.j, 456)
                .setExtension(Foobar.Bar.name, "simpleName")
        Foobar.Msg msg = Foobar.Msg.newBuilder().setFoo(foo).build();
        byte[] msgData = msg.toByteArray();

        // register extensions
        ExtensionRegistry registry = ExtensionRegistry.newInstance();

        final DescriptorProtos.FileDescriptorSet fds =

        Descriptors.Descriptor descriptor =
                Descriptors.FileDescriptor.buildFrom(fds.getFile(0), new 

        // build Msg from data using dynamically created descriptor
        DynamicMessage dynamicMessage = 
DynamicMessage.parseFrom(descriptor, msgData, registry);
        System.out.println("dynamically created descriptor: " + 
        // build Msg from data using manually created descriptor
        DynamicMessage dynamicMessage1 = 
DynamicMessage.parseFrom(Foobar.Msg.getDescriptor(), msgData, registry);
        System.out.println("manually created descriptor: " + 

dynamically created descriptor: {"foo": {"i": 123, "10001": [456], "10002": 
manually created descriptor: {"foo": {"i": 123,"Bar.j": 456,"Bar.name": 

So, dynamicMessage1 parsed correctly - it has fields "Bar.j" and "Bar.name" 
which were parsed as extensions.
But dynamicMessage parsed incorrectly - those fields parsed as unknown 
fields and their names are equal to thier numbers - "10001" and "10002".

May be problem is in getting descriptor for the "Msg" message? Is it 
correct way to get the descriptor dynamicaly?

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+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to