0

I have nullable enum in my code which I'm trying to define in my .proto file but when I generate the C# class the enum is not nullable. Here is my proto file

enum Foo {
   Bar = 0;
   Bat = 1;      
}

message FooBar {
  int32 Id = 1;
  optional Foo Foo = 2;
}

I've read about two possible solutions.

  1. Use oneof but this way I can't set the Bar to be 0
  2. Add Unknown = 0; but this is not backward compatible with the existing data where the 0 element has different meaning

Is there another options or it is not possible to make Nullable<Enum> as of now?

Thanks

zazka
  • 13
  • 3
  • https://stackoverflow.com/questions/4337193/how-to-set-enum-to-null Maybe this? –  Feb 04 '22 at 12:12
  • This implies touching the generated .cs file manually which is not viable because everytime I regenerate it using `protoc --csharp_out=. ./foobar.proto` I will have to edit it. – zazka Feb 04 '22 at 12:19
  • if 0 does not work for you as additional value hows -1? – Ralf Feb 04 '22 at 12:23
  • 2
    I would handle it as built in nullable value types are handled: create wrapper object for your enum and that can be null by default. See built in nullable value types: https://github.com/protocolbuffers/protobuf/blob/48234f5f012582843bb476ee3afef36cda94cb66/src/google/protobuf/wrappers.proto#L88 – fbede Feb 04 '22 at 12:24
  • public Foo? Foo = null; – Amjad S. Feb 04 '22 at 13:26

0 Answers0