Rob Komjathy created AVRO-4054:
----------------------------------

             Summary: Serialization of C# Dictionary only works if type is 
<string, object>
                 Key: AVRO-4054
                 URL: https://issues.apache.org/jira/browse/AVRO-4054
             Project: Apache Avro
          Issue Type: Bug
          Components: csharp
    Affects Versions: 1.12.0
         Environment: I was testing this in a .Net Standard 2.0 project.
            Reporter: Rob Komjathy


I encountered an unexpected behavior with Avro Maps (C# Dictionaries).  If I 
directly try to serialize a Dictionary<string, T> where T is any type besides 
literally "object", an exception is thrown. 

If you dig into the code, it's because you can't directly cast 
Dictionary<string, T> to Dictionary<string, object>.  This fails on at least 3 
lines in DefaultWriter, as there are several attempts made to verify the 
Dictionary "is" <string, object> or can be cast to it directly.

For example, here is a rudimentary example.

 
{code:java}
var schema = MapSchema.CreateMap(PrimitiveSchema.Create(Schema.Type.Long));
var ms = new MemoryStream();
Encoder enc = new BinaryEncoder(ms);
var writer = new DefaultWriter(schema);
writer.Write(new Dictionary<string, long>(), enc);
enc.Flush(); {code}
Given this example, I would expect to be able to pass a Dictionary<string, 
long> given the schema I have defined.  Instead this throws an exception. 

 

The work around is to create a new Dictionary<string, object> and individually 
cast the values to "objects".  For example:

 
{code:java}
public static Dictionary<string, object> ToAvroSafeDictionary<TValue>(this 
IDictionary<string, TValue> dictionary)
{
    return dictionary?.ToDictionary(k => k.Key, v => (object)v.Value);
} {code}
I originally encountered this with my own custom schema but as you can see, 
using Apache Avro's own schema objects in the rudimentary example exhibit the 
same issue.

 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to