Repository: avro Updated Branches: refs/heads/master ff5099c85 -> 5040c6413
AVRO-2115: Control commit. Project: http://git-wip-us.apache.org/repos/asf/avro/repo Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/c412267d Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/c412267d Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/c412267d Branch: refs/heads/master Commit: c412267d1d32ab10a929f189cb53cfb3cc619300 Parents: 7bbbf92 Author: Miguel Martinez-Espronceda <[email protected]> Authored: Wed Dec 13 11:19:15 2017 +0100 Committer: Miguel Martinez-Espronceda <[email protected]> Committed: Wed Dec 13 12:04:08 2017 +0100 ---------------------------------------------------------------------- .../org/apache/avro/reflect/ReflectData.java | 4 ++++ .../java/org/apache/avro/reflect/Union.java | 2 +- .../org/apache/avro/reflect/TestReflect.java | 25 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/avro/blob/c412267d/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java ---------------------------------------------------------------------- diff --git a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java index eb9f5fb..7220813 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java +++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java @@ -744,6 +744,10 @@ public class ReflectData extends SpecificData { if (explicit != null) // explicit schema return Schema.parse(explicit.value()); + Union union = field.getAnnotation(Union.class); + if (union != null) + return getAnnotatedUnion(union, names); + Schema schema = createSchema(field.getGenericType(), names); if (field.isAnnotationPresent(Stringable.class)) { // Stringable schema = Schema.create(Schema.Type.STRING); http://git-wip-us.apache.org/repos/asf/avro/blob/c412267d/lang/java/avro/src/main/java/org/apache/avro/reflect/Union.java ---------------------------------------------------------------------- diff --git a/lang/java/avro/src/main/java/org/apache/avro/reflect/Union.java b/lang/java/avro/src/main/java/org/apache/avro/reflect/Union.java index b22fa66..d823ebc 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/reflect/Union.java +++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/Union.java @@ -31,7 +31,7 @@ import java.lang.annotation.Target; * to a method, this determines its return type. */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.PARAMETER, ElementType.METHOD}) +@Target({ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) @Documented public @interface Union { /** The instantiable classes that compose this union. */ http://git-wip-us.apache.org/repos/asf/avro/blob/c412267d/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java ---------------------------------------------------------------------- diff --git a/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java b/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java index afe7f08..4d8a388 100644 --- a/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java +++ b/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java @@ -297,6 +297,31 @@ public class TestReflect { checkReadWrite(r9, ReflectData.get().getSchema(R9.class)); } + // test union in fields + public static class R9_1 { + @Union({R7.class, R8.class}) + public Object value; + @Override + public boolean equals(Object o) { + if (!(o instanceof R9_1)) return false; + return this.value.equals(((R9_1)o).value); + } + } + + @Test public void testR6_1() throws Exception { + R7 r7 = new R7(); + r7.value = 1; + checkReadWrite(r7, ReflectData.get().getSchema(R6.class)); + R8 r8 = new R8(); + r8.value = 1; + checkReadWrite(r8, ReflectData.get().getSchema(R6.class)); + R9_1 r9_1 = new R9_1(); + r9_1.value = r7; + checkReadWrite(r9_1, ReflectData.get().getSchema(R9_1.class)); + r9_1.value = r8; + checkReadWrite(r9_1, ReflectData.get().getSchema(R9_1.class)); + } + // test union annotation on methods and parameters public static interface P0 { @Union({Void.class,String.class})
