Skip to content

discrepancy between protobuf.js and various other encoders #581

@ghost

Description

Using protobuf.js version: 6.2.1 (latest)

When encoding messages which only contain optional properties with default values or null values, the protobuf.js library strips the message from the wire and thus the generated output is different.

Proto definition:

message Message 
{
  optional messageA MessageA = 1;
  optional messageB MessageB = 2;
}

message messageA
{
  optional bool valueA = 1;
}

message messageB 
{
  optional bool valueB = 1;
}

Buffer message:

{
  "MessageA": null,
  "MessageB": {
    "valueB": null
  }
}

The binary output of the library is "" (empty buffer).
Expected binary output is "0001 0010 0000 0000" (0x1200)

Another way to duplicate the behaviour is to decode the 0x1200 message with the library and the provided proto and then encoding it again, the output differs from the input. How can we configure the library to generate the expected output (0x1200)

Code extract:

var testBuffer  = Buffer.from('1200', 'hex');
var protoMsg    = root.lookup('Message');
var message     = protoMsg.decode(testBuffer); // Message contains subnodes MessageA and MessageB

var buffer = protoMsg.encode(message).finish(); // Buffer is empty after encoding

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions