ReflectDatumWriter failing with error "Array data must be a Collection or Array"

19 views Asked by At

I have a scala project which pulls in a library containing an avro schema and a set of case classes which were generated from that schema. I am trying to convert an object from a case class to a GenericRecord using ReflectDatumWriter. However, when I call write(), I get the following error:

org.apache.avro.AvroRuntimeException: Array data must be a Collection or Array
    at org.apache.avro.reflect.ReflectDatumWriter.writeArray(ReflectDatumWriter.java:76)

This appears to be caused when trying to get the class of a List of objects that are the type of a case class. The definition of the relevant case classes are:

case class MessageType(
    var Link: List[LinkType] = null
  ) {
  def this() = this(null)
}

case class LinkType(
    var Description: String = null,
    var Url: String = null
  ) {
  def this() = this(null,null)
}

I have tried setting Link as an empty List[LinkType], a List[LinkType] with one new Link, and a list with a Link with populated values. The line in ReflectDatumWriter.java that is causing the error is:

Class<?> elementClass = datum.getClass().getComponentType();

where datum should be a List[LinkType] but appears in the debugger as a $colon$colon and elementClass ends up set to null. Is there some way I can modify this so that datum.getClass().getComponentType() returns a class instead of null?

If it's helpful, here is the avro schema for the LinkType:

{
    "name": "Link",
    "type": {
        "type": "array",
        "items": {
            "type": "record",
            "name": "LinkType",
            "fields": [
                {
                    "name": "Description",
                    "type": [
                        "null",
                        "string"
                    ],
                    "default": null
                },
                {
                    "name": "Url",
                    "type": [
                        "null",
                        "string"
                    ],
                    "default": null
                }
            ]
        }
    }
}
0

There are 0 answers