bitwalker / exprotobuf

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

Decoding not existing oneof field should return an error #94

Open pallix opened 5 years ago

pallix commented 5 years ago

When decoding a message using a oneof with a field that has not been defined by the code doing the decoding, no error is thrown. Instead, the field has the nil value. It would be better if the decode function would fail with an explicit error instead of giving an invalid result.

Example:

one_of.proto


message SampleOneofMsg {
  optional string one = 1;

  oneof foo {
    string body = 3;
    uint32 code = 4;
  }
}

message SampleOneofExtMsg {
  optional string one = 1;

  oneof foo {
    string body = 3;
    uint32 code = 4;
    string meta = 5;
  }
}

This code:

    msg_ext = Msgs.SampleOneofExtMsg.new(foo: {:meta, "meta"}, one: "test")
    enc_msg = Protobuf.Serializable.serialize(msg_ext)

    IO.inspect(Msgs.SampleOneofMsg.decode(enc_msg))

will return:

%Protobuf.Oneof.Test.Msgs.SampleOneofMsg{foo: nil, one: "test"}
pallix commented 5 years ago

It would be better if the decode function would fail with an explicit error instead of giving an invalid result.

Thinking more about it, a failure would not be preferable as this would prevent to decode the message at all: some applications may want to retrieve the available data even if the oneof field cannot be decoded.

The real problem is that it is not possible to distinguish between a missing oneof field and an undecodable oneof field.