[ https://issues.apache.org/jira/browse/AVRO-2115?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16287765#comment-16287765 ]
Miguel edited comment on AVRO-2115 at 12/13/17 8:05 AM: -------------------------------------------------------- This should do the job: {code:java} public class ReflectDataAllowingUnionsInMember extends ReflectData { @Override protected Schema createSchema(final Type type, final Map<String, Schema> names) { if (type instanceof Class<?>) { Class<?> cl = (Class<?>) type; if (cl.isAnnotationPresent(Union.class)) { Union annotation = cl.getAnnotation(Union.class); Class<?>[] list = annotation.value(); if (list != null) { List<Schema> branches = new ArrayList<>(); for (Class<?> branch : list) { final Schema schema; if (branch == null) { schema = Schema.create(Schema.Type.NULL); } else { schema = createSchema(branch, names); } branches.add(schema); } return Schema.createUnion(branches); } } } return super.createSchema(type, names); } } {code} I think this must be inside the createSchema method within the ReflectData class. *#edit* I omitted that also need to add the ElementType.FIELD target in @Union. {code:java} @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD}) @Documented public @interface Union { /** The instantiable classes that compose this union. */ Class[] value(); } {code} was (Author: elmendavies): This should do the job: {code:java} public class ReflectDataAllowingUnionsInMember extends ReflectData { @Override protected Schema createSchema(final Type type, final Map<String, Schema> names) { if (type instanceof Class<?>) { Class<?> cl = (Class<?>) type; if (cl.isAnnotationPresent(Union.class)) { Union annotation = cl.getAnnotation(Union.class); Class<?>[] list = annotation.value(); if (list != null) { List<Schema> branches = new ArrayList<>(); for (Class<?> branch : list) { final Schema schema; if (branch == null) { schema = Schema.create(Schema.Type.NULL); } else { schema = createSchema(branch, names); } branches.add(schema); } return Schema.createUnion(branches); } } } return super.createSchema(type, names); } } {code} I think this must be inside the createSchema method within the ReflectData class. > Support @Union in members too > ----------------------------- > > Key: AVRO-2115 > URL: https://issues.apache.org/jira/browse/AVRO-2115 > Project: Avro > Issue Type: Improvement > Reporter: Miguel > Priority: Minor > > Allow defining unions in members: > {code:java} > class MyDatum { > @Nullable > @Union({String.class, Long.class, Other.class,...}) > Object o; > } > {code} > It can currently be implemented using @AvroSchema like this, but it is seems > in my oppinion more verbose and more difficult to maintain: > {code:java} > @AvroSchema("[\"null\",\"string\",...") > {code} -- This message was sent by Atlassian JIRA (v6.4.14#64029)