Hello Avro community,

I want to use my Avro serialized data with .net and I stumbled upon a strange 
problem. When a record in my schema references another record, and I set it to 
null, when serializing this in .net this throws an exception.

Here is what I did:

First, I created a simple protocol. This is my avdl file:

@namespace("sample")

protocol SampleAvro {

record Sample {

  string foo;
  Sample bar;
}
}

Now, I generated C# classes out of it:


java -jar avro-tools-1.8.0.jar idl sample.avdl >sample.json

avrogen -p sample.json .

I then copied the .cs files into a project, and tried to serialize a sample 
object where the "bar" reference is null:


        using(var ms = new MemoryStream()) {

            var sw = new SpecificWriter<Sample>(Sample._SCHEMA);

            sw.Write(new Sample { foo = "bar" }, new BinaryEncoder(ms));

        }

I get the following exception: Record object is not derived from 
ISpecificRecord in field bar

When looking at the source code, this happens in the SpecificWriter.cs in the 
following method (I made the relevant lines bold):


        protected override void WriteRecord(RecordSchema schema, object value, 
Encoder encoder)

        {

            var rec = value as ISpecificRecord;

            if (rec == null)

                throw new AvroTypeException("Record object is not derived from 
ISpecificRecord");



            foreach (Field field in schema)

            {

                try

                {

                    Write(field.Schema, rec.Get(field.Pos), encoder);

                }

                catch (Exception ex)

                {

                    throw new AvroException(ex.Message + " in field " + 
field.Name);

                }

            }

        }

The "value as ISpecificRecord" returns null here because the reference is null. 
This is intended in my schema, but throws an exception here.

Is this a bug or is this intended behavior, and either way, how should I work 
around this? I need situations where the "bar" is null, and on the JVM/Scala 
side this is no problem and working flawlessly.

Regards,
Jens

Reply via email to