You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by S G <sg...@gmail.com> on 2018/12/19 07:21:04 UTC
Re: Avro Inheritance and Polymorphism Behavior
Chris,
This is something which was reported long back via
https://issues.apache.org/jira/browse/AVRO-1568
But no action has been taken on this since then.
I guess no one uses Avro for such complex cases.
-SG
On Tue, Oct 30, 2018 at 7:35 AM Christopher Dorick <to...@vt.edu> wrote:
> Hello,
>
>
>
> I am attempting to implement an Apache Avro schema with inheritance and
> polymorphism and are running into an issue. *We would like to be able to
> serialize records that include either an implementation of the base class
> or the child class.*
>
>
>
> This can best be explained in the following example:
>
> Assume we have a class Student. There are 2 classes that extend Student:
> ScienceMajor and MathMajor. Upon implementation, if a Student is undeclared
> in his/her degree, they are simply of type Student. Once they declare
> major, they will become one of the Major types.
>
>
>
> We have tried several different implementations to achieve our goal...
>
> 1. Using the annotation * @Union(value = {ScienceMajor.class,
> MathMajor.class}) *on Student
>
> a. This results in a valid schema being generated, but no semblance
> of a field for Student.
>
> b. We are able to serialize successfully when passing xMajor
> objects, but serialization fails when we attempt to pass in Student object.
>
> c. This can be seen when attempting to generate a serialized record
> from a schema generated from file Student.java
>
> 2. Using the union annotation *@Union(value = {ScienceMajor.class,
> MathMajor.class, Sudent.class}) *on student
>
> a. This results in a stack overflow on schema generation – which
> makes sense because each call is getting a union annotation on itself.
>
> b. Not able to generate schema
>
> 3. Using the annotation * @Union(value = {ScienceMajor.class,
> MathMajor.class}) *on Student and manually adding *Student.class* field
> to the schema
>
> a. This results in a serialization error when trying to serialize
> either xMajor or Student.
>
>
>
> Schema Generation Code Used:
>
> Line 17 of Main.java
>
> Serialization Code Used:
>
> Lines 22-26 of Main.java
>
>
>
> After doing a fair bit of googling on this issue and not finding much, I
> am beginning to think this is a known edge case that is not implemented
> yet. Is this currently supported or is there an annotation that we need to
> add that I am glossing over?
>
>
>
> Thanks,
>
> Christopher Dorick
>
>