Skip to content

Incorrect encoding of nested empty messages. #585

@kfir-drivenets

Description

@kfir-drivenets

protobuf.js version: 6.3.0

protobuf js isn't encoding correctly messages with nested empty messages.

protobuf used:

syntax = 'proto2';          
                            
message Empty {             
                            
}                           
                            
message A {                 
    optional int32 a = 1;   
}                           
                            
message B {                 
    oneof child {           
        A a = 1;            
        Empty b = 2;        
    }                       
}

Snippet to reproduce:

root = protobuf.parse(protoString).root;
var Empty = root.lookup("Empty");
var A = root.lookup("A");
var B = root.lookup("B");
var msg = B.from({
	b: {}
});
console.log("msg", msg);
var buffer = B.encode(msg).finish();
console.log(buffer); // Buffer is empty!, should be [18, 00]

console.log(B.decode(new Uint8Array(18, 0))); // Should be equal to msg

jsfiddle link: https://jsfiddle.net/13c0re75/

Using the python binding for protobuf I got a different result.
Note: I used SetInParent - http://stackoverflow.com/questions/29643295/how-to-set-a-protobuf-field-which-is-an-empty-message-in-python

import example_pb2
import binascii

msg = example_pb2.B()
msg.b.SetInParent()
print "msg: ", msg
print "msg buffer: (hex string)", binascii.hexlify(msg.SerializeToString())

The result of the previous snippet is

msg:  b {
}

msg buffer: (hex string) 1200

I've verified the same behavior with another binding of protobuf (https://github.com/protobuf-c/protobuf-c) and got the same results.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions