[
https://issues.apache.org/jira/browse/AVRO-3583?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17632425#comment-17632425
]
Oscar Westra van Holthe - Kind commented on AVRO-3583:
------------------------------------------------------
As far as I know this is not intended, but also tricky to solve: if classes are
evaluated in another order, this changes which classes are available. This is
especially true for cyclic references.
IMHO, this would be an excellent change. I just don't see a way to implement it.
> Avro ReflectData API does not allow referencing external schemas through
> @AvroSchema
> ------------------------------------------------------------------------------------
>
> Key: AVRO-3583
> URL: https://issues.apache.org/jira/browse/AVRO-3583
> Project: Apache Avro
> Issue Type: Bug
> Components: java
> Affects Versions: 1.10.2
> Reporter: Filip
> Priority: Major
>
> For easier transitioning into an Avro-based project, the {{ReflectData}} API
> allows the generation of {{.avsc}} Schema files from POJOs.
> However, currently it does not seem to be possible to reference external
> (from the point of view of the POJO) schemas when doing so. Particularly, the
> following rather trivial case does not seem covered:
> {code:java}
> package com.acme;
> public class Seller {
>
> private String name;
> @AvroSchema("{\"type\":\"com.acme.Address\",\"name\":\"sellerAddress\"}")
> private Address sellerAddress;
> // getters/setters/constructors
> }
> // separate file
> package com.acme;
> public class Address {
>
> private String street;
> private String streetNumber;
> // ...
> // getters/setters/constructors
> }{code}
> trying to use e.g.:
> {code:java}
> ReflectData.AllowNull.get().getSchema(Seller.class)
> {code}
> currently throws the following exception:
> {code:java}
> org.apache.avro.SchemaParseException: Type not supported:
> com.acme.Address{code}
> {*}Note{*}: this does not have anything to do with forward references (which
> are disallowed by Avro). The problem lies in
> {{org.apache.avro.reflect.ReflectData#createFieldSchema}} which contains the
> following code:
> {code:java}
> 865 AvroSchema explicit = field.getAnnotation(AvroSchema.class);
> 866 if (explicit != null) // explicit schema
> 867 return new Schema.Parser().parse(explicit.value()); {code}
> which explicitly (no pun intended) creates a new {{Parser}} instance which is
> *not aware* of any other schema definitions that may have been processed
> previously.
> Not using {{@AvroSchema}} is obviously a possibility, but:
> # Any POJO with more than a few fields will be needlessly complex and long,
> # Care must be taken *not to reparse* the referenced class, since it would
> lead to a redefinition of the schema and fail.
> Is this intended behaviour? Is there a better way to generate schema files
> from existing POJOs for non-trivial classes hierarchies? We were looking at
> potentially switching to Avro, but I currently do not see a way of generating
> the required schema files, even with the {{ReflectData}} API...
--
This message was sent by Atlassian Jira
(v8.20.10#820010)