You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Swamy Thota <sw...@gmail.com> on 2022/01/28 00:41:10 UTC

Reflection Based Serializer on an Interface

Hi All,

I have POJO model where I have an interface which doesn't have any fields
defined, but the actual implementation determined during runtime has
several fields defined. When I generate the schema using reflection it
doesn't take into account of the actual implementation. When I try to
serialize the POJO it's throwing an exception saying it doesn't know
specific fields in the implementation. How do I work around this? Is there
a way to hint the Serializer to use specific implementation that contains
fields?

Thanks,
Swamy

Re: Reflection Based Serializer on an Interface

Posted by Ryan Skraba <ry...@skraba.com>.
Hello!  I was hoping someone has better news, but I'm afraid there's a
couple of constraints in using interfaces with ReflectData.

My recommendation would be to create a Schema from your actual
concrete implementation, and drop it onto your interface with an
@AvroSchema annotation.  It's not necessarily the best solution,
because the name of the schema will (and must) be the concrete
implementation.

I put an example here:
https://github.com/RyanSkraba/avro-enchiridion/blob/c1951937661390ca6365033aaae12d2c9e8a6a20/core/src/test/java/com/skraba/avro/enchiridion/core/ReflectDataTest.java#L110

In that test case, the Issue interface is annotated with the schema
that you would get from ReflectData.get().getSchema(IssueImpl.class),
and you would *have* to keep the two in sync.

I hope this is helpful, Ryan

On Fri, Jan 28, 2022 at 1:41 AM Swamy Thota <sw...@gmail.com> wrote:
>
> Hi All,
>
> I have POJO model where I have an interface which doesn't have any fields defined, but the actual implementation determined during runtime has several fields defined. When I generate the schema using reflection it doesn't take into account of the actual implementation. When I try to serialize the POJO it's throwing an exception saying it doesn't know specific fields in the implementation. How do I work around this? Is there a way to hint the Serializer to use specific implementation that contains fields?
>
> Thanks,
> Swamy