Closed ievgenii-shepeliuk closed 2 years ago
At the moment, code generation only supports nullable value types (note the bool?
above), not reference types. We plan to fully support nullable reference types in 8.x.
That said, enums are value types and thus should be supported currently; #171 fixes that bug and will be included in the next 7.2.x.
Looking forward new release, thanks for fixing it.
@dstelljes is it possible to somehow hack it on serialization level, so even if fields is not nullable in C# class - when value of reference type is null - it will be serialized as null
?
Yes, that’s how Chr.Avro currently behaves—the library is oblivious to C# 8+ nullable reference types.
Can you provide an example ? I have an exception when serializing messages.
Although I have more complicated setup - i am wrapping Chr.Avro serializer to use it together with https://github.com/Farfetch/kafka-flow
I am creating an instance
_serializer = new AsyncSchemaRegistrySerializer<T>(schemaRegistryClient);
and then invoke SerializeAsync()
Sure, say I’m using your schema:
{
"name": "Models.MyClass",
"type": "record",
"fields": [
{
"name": "RecordField",
"type": ["null", {
"name": "Models.Credentials",
"type": "record",
"fields": [
{"name": "Password", "type": "string"},
{"name": "Username", "type": "string"}
]
}]
}
]
}
and mapping it to these classes:
#nullable disable // turn off nullable reference types to match Chr.Avro
namespace Models
{
public class Credentials
{
public string Password { get; set; }
public string Username { get; set; }
}
public class MyClass
{
public Credentials RecordField { get; set; }
}
}
the serializer would allow me to do this:
var context = new SerializationContext(MessageComponentType.Value, "example-topic");
// this should work because `RecordField` is `["null", ...]`
await _serializer.SerializeAsync(new MyClass { RecordField = null }, context);
but not this:
var context = new SerializationContext(MessageComponentType.Value, "example-topic");
// this would not work because `Password` is not `["null", ...]`
await _serializer.SerializeAsync(new MyClass { RecordField = new Credentials { Username = "user", Password = null } }, context);
Yes, worked. Thanks for your help !
Full nullable reference type support is released with 8.0.0-pre.1.
Hello
I'm using
dotnet avro generate
to create C# class from Avro schema.My schema looks like
I expect that properties
BoolField
,StringField
,RecordField
,EnumField
will be nullable in generated class. But they are not. The resulting C# class looks likeAm I missing smth or this is a bug in code generation ?