When generating code for C# with protoc, the sub-messages used in the messages defined in the proto files are not marked as nullable (the ?
, used along with the csproj
option <Nullable>enable</Nullable>
).
I have seen that, even if they are not marked as nullable, these properties can still receive unexpected null
objects.
I'm wondering if it is possible to ask protoc
to declare the type of the properties exposing sub-messages as nullable.
I tried to find an option in protoc to do so but failed.
What I expect is the code generated in C# clearly exposing that the sub-messages are nullable:
message Main {
Sub sub = 1;
}
message Sub {
int id = 1;
}
class Main {
Sub? Sub { get; set; }
}
class Sub {
int Id { get; set; }
}
I'm wondering if it is possible to ask protoc to declare the type of the properties exposing sub-messages as nullable.
Not at the moment, no.
There is some effort on this front, but nothing has landed yet. With limited resources and a lot of work on multiple fronts, I can't give any estimates as to when we might be able to land it (along with nullable annotations for the Google.Protobuf library).
There are proposals you may want to look at for protoc and for the gRPC plugin - if you subscribe to those PRs, then even if they're not merged directly, I expect you'll see indications of any progress that does happen.