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

Possible incorrect oneof serealization #102

Open
farwayer opened this issue May 11, 2019 · 0 comments
Open

Possible incorrect oneof serealization #102

farwayer opened this issue May 11, 2019 · 0 comments
Labels

Comments

@farwayer
Copy link

Library should serialize default values of oneof field in any case to be possible to understand which one was set. Decoding fix for oneof was done in #51 for #54. But encoding is still incorrect. Comparison with other libraries:

message Id {
  oneof type {
    uint64 numId = 1;
    double doubleId = 2;
    string strId = 3;
  }
}
const protobufjs = require('protobufjs');
const google_pb = require('../gen/rpc_pb');
const Pbf = require('pbf');
const {Id} = require('./rpc');

const Data = {numId: 0};

function testGoogle() {
  const id = new google_pb.Id();
  id.setNumid(Data.numId);
  const s = id.serializeBinary();

  const d = google_pb.Id.deserializeBinary(s);

  console.log(
    'google-protobuf',
    Buffer.from(s),
    d.getTypeCase() === google_pb.Id.TypeCase.NUMID,
  );
}

async function testProtobufjs() {
  const root = await protobufjs.load('./src/rpc.proto');
  const Id = root.lookupType('Id');
  const data = Id.create(Data);
  const s = Id.encode(data).finish();

  const d = Id.decode(s);

  console.log(
    'protobufjs',
    s,
    d.type === 'numId',
  );
}

function testPbf() {
  const pbf = new Pbf();
  Id.write(Data, pbf);
  const s = pbf.finish();

  const d = Id.read(new Pbf(s));

  console.log(
    'pbf',
    Buffer.from(s),
    d.type === 'numId',
  );
}

async function test() {
  testGoogle();
  await testProtobufjs();
  testPbf();
}

test();

Result:

google-protobuf <Buffer 08 00> true
protobufjs <Buffer 08 00> true
pbf <Buffer > false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants