Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Nullable Uri #277

Closed
kevin-mcmanus opened this issue Oct 3, 2023 · 1 comment · Fixed by #278
Closed

Support for Nullable Uri #277

kevin-mcmanus opened this issue Oct 3, 2023 · 1 comment · Fixed by #278
Assignees
Labels
bug Something isn't working
Milestone

Comments

@kevin-mcmanus
Copy link

I'm trying to serialize/deserialize a class containing a nullable Uri property (Uri?). Everything works as expected when the Uri is populated, but when it is null I get a NullReferenceException.

Looking at StringSerializerBuilderCase, Uri is the only special case that's a reference type, where as the others (DateTime, Guid, etc.) are all structs and behave well.

What is the best way to serialize a null Uri correctly?

This code reproduces the issue:

    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            var schemaBuilder = new SchemaBuilder(nullableReferenceTypeBehavior: NullableReferenceTypeBehavior.All);
            var schema = schemaBuilder.BuildSchema<Class1>();
            var serdes = new Chr.Avro.Serialization.BinarySerializerBuilder().BuildDelegate<Class1>(schema);

            using (var stream = new MemoryStream())
            {
                using (var writer = new Chr.Avro.Serialization.BinaryWriter(stream))
                {
                    serdes.Invoke(new Class1(), writer);
                }
            }

            // ...
        }
    }

    public class Class1
    {
        public Uri? Uri { get; set; }
    }

It throws:

System.NullReferenceException
Object reference not set to an instance of an object.
   at Class1 to Class1 serializer(Closure, Class1, BinaryWriter)
   at ChrAvroNullUri.UnitTest1.Test1() in C:\...\UnitTest1.cs:line 22
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
@dstelljes dstelljes self-assigned this Oct 3, 2023
@dstelljes dstelljes added the bug Something isn't working label Oct 3, 2023
@dstelljes
Copy link
Member

Thanks for the detailed reproduction! This turned out to be an issue with the schema builder; a "string" schema was generated for Uri? instead of ["null", "string"]. The fix will ship with the next release.

@dstelljes dstelljes added this to the 10.0.0 milestone Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants