avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeremy Fine <jeremy.f...@intentmedia.com>
Subject Re: When using the SpecificWriter with .net, I get an exception when a referenced object in my schema is null
Date Tue, 17 Jan 2017 15:11:46 GMT
does avdl this work? i think you need to specify that a field can be null.


@namespace("sample")



protocol SampleAvro {



record Sample {



  union { null, string } foo;

  union { null, Sample } bar;

}

}




On Mon, Jan 16, 2017 at 1:01 PM, Rabe, Jens <jens.rabe@iwes.fraunhofer.de>
wrote:

> Hello again,
>
>
>
> it turns out I was wrong in first place. I re-tested it in Scala and get
> the same error.
>
>
>
> Gues I’ll have to redesign the schema then to avoid the nulls.
>
>
>
> *Von:* Rabe, Jens [mailto:jens.rabe@iwes.fraunhofer.de]
> *Gesendet:* Montag, 16. Januar 2017 18:13
> *An:* user@avro.apache.org
> *Betreff:* When using the SpecificWriter with .net, I get an exception
> when a referenced object in my schema is null
>
>
>
> 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
>



-- 

[image: Intent Media] <http://IntentMedia.com>

*Jeremy Fine*
Software Engineer
315 Hudson Street, 9th Floor, New York, NY 10013
We've been named a Crain's "Best Place to Work" for three years running!
<http://IntentMedia.com/jobs>

Mime
View raw message