Details
-
Improvement
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
1.10.2
-
None
-
None
Description
Since Avro-schemas allow describing nullable types by wrapping them in a union with the null type, it would be great if that information wasn't lost when generating types for C#.
With C# 8 onwards there's (optional) support for nullable reference types there, through which you get compile-time-safe variables, fields, properties, method parameters and return types, which cannot be null, by default. If you want to be able to assign null there, you have to add a ?. You can get more information on that [here|https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references]. Since it's optional and not all preexisting projects have opted in to it (yet), it would need to be optional during code generation as well.
As far as I can see, this could be easily facilitated by adding a property UseNullableReferenceTypes to the CodeGen class and change https://github.com/apache/avro/blob/master/lang/csharp/src/apache/main/CodeGen/CodeGen.cs#L755 to return CodeGenUtil.Instance.Mangle(namedSchema.Fullname) + (UseNullableReferenceTypes && nullable ? "?" : "");. The CodeGen CLI-tool would also need to have a flag to pass on.
I would appreciate it if this could be considered "in scope" for this library and I'd be willing and able to provide a patch and prepare test-cases for it.