Zachary Payne created AVRO-4274:
-----------------------------------

             Summary: Add Support for Java Generics in ReflectData
                 Key: AVRO-4274
                 URL: https://issues.apache.org/jira/browse/AVRO-4274
             Project: Apache Avro
          Issue Type: New Feature
          Components: java
            Reporter: Zachary Payne


{{ReflectData}} generates schema by inspecting a class via reflection; when a 
field's declared type is a parameterized type or a type variable (e.g. {{T}} 
from a class {{{}Box<T>{}}}), {{ReflectData}} has no way to resolve those type 
variables to their concrete types. The result is either a runtime error or a 
schema that loses the type information entirely.

My team has created an extended Avro Java library internally, and we've 
produced a working implementation of this approach and have been running it in 
production. Our extension includes single-level generics, multi-level nesting, 
multiple type parameters, sealed class hierarchies, and union fields with 
generic members. We'd like to contribute this extension of Avro back to the 
open source code.

All changes are additive. Existing non-generic classes follow the same code 
paths as today.

*Current Results:* (requires a concrete class {{{}HierarchyAString{}}})
{code:java}
{
  "type" : "record",
  "name" : "PojoWithGenericWithString",
  "namespace" : "org.apache.avro.reflect.TestReflect",
  "fields" : [ {
    "name" : "myHierarchyA",
    "type" : {
      "type" : "record",
      "name" : "HierarchyAString",
      "fields" : [ {
        "name" : "value",
        "type" : "string"
      } ]
    }
  } ]
}
{code}
*Our Approach:* (supports a generic instead of a concrete class)
{code:java}
{
  "type" : "record",
  "name" : "PojoWithGenericWithString",
  "namespace" : "[...].avro.model.sample.PojoGenerics",
  "fields" : [ {
    "name" : "myHierarchyA",
    "type" : {
      "type" : "record",
      "name" : "HierarchyA_LB_String_RB_",
      "fields" : [ {
        "name" : "value",
        "type" : "string"
      } ],
      "className" : "HierarchyA"
    }
  } ]
}
{code}



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

Reply via email to