Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect encoding of nested empty messages. #585

Closed
kfir-drivenets opened this issue Dec 27, 2016 · 2 comments
Closed

Incorrect encoding of nested empty messages. #585

kfir-drivenets opened this issue Dec 27, 2016 · 2 comments

Comments

@kfir-drivenets
Copy link

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.

dcodeIO added a commit that referenced this issue Dec 27, 2016
…e + removed now unused Writer#ldelim parameter, see #585
@dcodeIO
Copy link
Member

dcodeIO commented Dec 27, 2016

Let me know if this fixes it. The test case is somewhat simplified but should still catch it, theoretically.

@kfir-drivenets
Copy link
Author

Works like a charm. Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants