Skip to content

Problem with required enum field with default value #103

Open
@TeunVR

Description

@TeunVR

Consider the following .proto file:

message TestMessage {
    enum Result {
        OK = 0;
        FAILED = 1;
        TIMEOUT = 2;
    }
    required Result result = 1 [default = OK];
}

I would expect to be able to build a new TestMessage with:

TestMessage msg = TestMessage.CreateBuilder().Build();

This fails with:
An unhandled exception of type 'Google.ProtocolBuffers.UninitializedMessageException' occurred in Google.ProtocolBuffers.dll
Additional information: Message missing required fields: result

If i look at the generated code, the result-field is correctly set. However, the hasResult boolean is not set to true.

  public const int ResultFieldNumber = 1;
  private bool hasResult;
  private global::TestMessage.Types.Result result_ = global::TestMessage.Types.Result.OK;
  public bool HasResult {
    get { return hasResult; }
  }
  public global::TestMessage.Types.Result Result {
    get { return result_; }
  }

  public override bool IsInitialized {
    get {
      if (!hasResult) return false;
      return true;
    }
  }

I would expect the result field to have a default value, so i think hasResult should be initialized to true if a default value is set.

Note: using version 2.4.1.555

Or should this issue be submitted in https://github.com/google/protobuf ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions