You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Fabian Grewing (Jira)" <ji...@apache.org> on 2021/05/12 08:26:00 UTC

[jira] [Created] (AVRO-3136) Support C#'s nullable reference types in CodeGen

Fabian Grewing created AVRO-3136:
------------------------------------

             Summary: Support C#'s nullable reference types in CodeGen
                 Key: AVRO-3136
                 URL: https://issues.apache.org/jira/browse/AVRO-3136
             Project: Apache Avro
          Issue Type: Improvement
          Components: csharp
    Affects Versions: 1.10.2
            Reporter: Fabian Grewing


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.

 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)