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

Setting the default value to a string field causes a bad result #1357

Closed
toshirin33 opened this issue Feb 6, 2020 · 5 comments
Closed

Setting the default value to a string field causes a bad result #1357

toshirin33 opened this issue Feb 6, 2020 · 5 comments

Comments

@toshirin33
Copy link

toshirin33 commented Feb 6, 2020

protobuf.js version: +-- [email protected]

When the default value for string, empty string represented by '' or "" is set to a string field,
the serialized result is different from the one the value is not set.

tmp.protoc

syntax = "proto3";
package protobuf;

message tuple {
  string f1 = 1;
  string f2 = 2;
}

poi.js

let protobuf = require('protobufjs');

function output_pb(tuple, payload){
    let errMsg = tuple.verify(payload);
    if (errMsg)
        throw Error(errMsg)

    let obj = tuple.create(payload);

    let buf = tuple.encode(obj).finish();

    console.log(buf.toString("hex"));
}

protobuf.load("tmp.proto", function(err, root){
    let tuple = root.lookupType("protobuf.tuple");

    let payload = {f1: "hello"};
    output_pb(tuple, payload);

    payload = {f1: "hello", f2:""};
    output_pb(tuple, payload);
    
})

Output is the following

$ node poi.js
0a0568656c6c6f
0a0568656c6c6f1200
@toshirin33 toshirin33 changed the title Setting a default value to a string field causes a bad result Setting the default value to a string field causes a bad result Feb 6, 2020
@toshirin33
Copy link
Author

The result was valid . I have noticed that the encoded values give the same decoded value.
I thought the serialized value is unique but it is not true.

@toshirin33
Copy link
Author

In https://developers.google.com/protocol-buffers/docs/proto3#default I found the following sentence.

Also note that if a scalar message field is set to its default, the value will not be serialized on the wire.

I think the result 0a0568656c6c6f1200 should be 0a0568656c6c6f .

@toshirin33 toshirin33 reopened this Feb 14, 2020
@webmaster128
Copy link
Contributor

Thank you for pointing this out. I agree this is a bug in the encoder. The same problem exist at least for numbers as well.

@webmaster128
Copy link
Contributor

This is pretty much a duplicate of #1118 (just for string instead of bool)

@toshirin33
Copy link
Author

Thank you for showing #1118. I agree that this issue is a duplicate of that, so I close this issue.

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

No branches or pull requests

2 participants