bitwalker / exprotobuf

Protocol Buffers in Elixir made easy!
Apache License 2.0
486 stars 69 forks source link

Encoding/Decoding optional string turns nil into empty string (in/for proto3). #101

Closed rolandtritsch closed 5 years ago

rolandtritsch commented 5 years ago

Hi All,

just stumbled over this testcase.

The proto2 behavior makes sense to me.

The proto3 behavior not so much.

Is this a bug?

Regards ... Roland

relistan commented 5 years ago

I think you are calling out that in protobuf3 you get empty strings back and not nils when the input value was nil, right @rolandtritsch? This error from the build:

     test/protobuf_test.exs:142
     Assertion with == failed
     code:  assert basic == decoded
     left:  %ProtobufTest.Basic{f1: 1, f2: nil}
     right: %ProtobufTest.Basic{f1: 1, f2: ""}
     stacktrace:
       test/protobuf_test.exs:149: (test)
rolandtritsch commented 5 years ago

@bitwalker @relistan Sorry. Yes. Correct :).

rolandtritsch commented 5 years ago

@relistan BTW ... this not only fails for strings. It fails for all optional fields (e.g. for integers a field with value 0 will show up out of nowhere :)).

rolandtritsch commented 5 years ago

https://developers.google.com/protocol-buffers/docs/proto3#default

Interesting ...

When you research this a little bit more, then you will find out that nils and all default values for all data scalar types (e.g. empty string, 0, etc.) are not serialized to the wire and that on the way back all missing fields are initialized with the default values (because by the time you decode/de-serialize a buffer, you cannot figure out anymore if the field is not there because it was nil or the default value).

Exec Summary: The implemented behavior is correct. I am closing this PR. (cc: @relistan)