[
https://issues.apache.org/jira/browse/AVRO-3136?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Fabian Grewing updated AVRO-3136:
---------------------------------
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.
was:
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.
> 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
> Priority: Minor
> Original Estimate: 24h
> Remaining Estimate: 24h
>
> 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)