exprotobuf icon indicating copy to clipboard operation
exprotobuf copied to clipboard

Decoding not existing oneof field should return an error

Open pallix opened this issue 6 years ago • 1 comments

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 avatar Jan 08 '19 14:01 pallix

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.

pallix avatar Jan 09 '19 09:01 pallix