This is an automated email from the ASF dual-hosted git repository.

RyanSkraba pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/main by this push:
     new 4c29cfb234 AVRO-4211: Java fix schema generation for union fields with 
default value that isn't the first union element (#3709)
4c29cfb234 is described below

commit 4c29cfb234c6ca0d9df8a572ca04b65ccc7c21e4
Author: zakpayne8283 <[email protected]>
AuthorDate: Mon May 11 14:55:41 2026 -0400

    AVRO-4211: Java fix schema generation for union fields with default value 
that isn't the first union element (#3709)
---
 .../apache/avro/util/internal/JacksonUtils.java    | 22 +++++++++++++++++-----
 .../avro/util/internal/TestJacksonUtils.java       |  5 +++++
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git 
a/lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java 
b/lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java
index 29593fba09..c085997aab 100644
--- 
a/lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java
+++ 
b/lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java
@@ -114,14 +114,26 @@ public class JacksonUtils {
   }
 
   public static Object toObject(JsonNode jsonNode, Schema schema) {
-    if (schema != null && schema.getType().equals(Schema.Type.UNION)) {
-      return toObject(jsonNode, schema.getTypes().get(0));
-    }
     if (jsonNode == null) {
       return null;
     } else if (jsonNode.isNull()) {
       return JsonProperties.NULL_VALUE;
-    } else if (jsonNode.isBoolean()) {
+    }
+
+    if (schema != null && schema.getType().equals(Schema.Type.UNION)) {
+      for (Schema unionType : schema.getTypes()) {
+        if (unionType.getType().equals(Schema.Type.NULL)) {
+          continue;
+        }
+        Object unionObject = toObject(jsonNode, unionType);
+        if (unionObject != null) {
+          return unionObject;
+        }
+      }
+      return null;
+    }
+
+    if (jsonNode.isBoolean()) {
       return jsonNode.asBoolean();
     } else if (jsonNode.isInt()) {
       if (schema == null || schema.getType().equals(Schema.Type.INT)) {
@@ -187,7 +199,7 @@ public class JacksonUtils {
 
   /**
    * Convert an object into a map
-   * 
+   *
    * @param datum The object
    * @return Its Map representation
    */
diff --git 
a/lang/java/avro/src/test/java/org/apache/avro/util/internal/TestJacksonUtils.java
 
b/lang/java/avro/src/test/java/org/apache/avro/util/internal/TestJacksonUtils.java
index cd6fe3f74d..314685f43d 100644
--- 
a/lang/java/avro/src/test/java/org/apache/avro/util/internal/TestJacksonUtils.java
+++ 
b/lang/java/avro/src/test/java/org/apache/avro/util/internal/TestJacksonUtils.java
@@ -100,6 +100,11 @@ public class TestJacksonUtils {
         toObject(NullNode.getInstance(), 
SchemaBuilder.unionOf().nullType().and().intType().endUnion()));
 
     assertEquals("a", toObject(TextNode.valueOf("a"), 
SchemaBuilder.unionOf().stringType().and().intType().endUnion()));
+
+    assertEquals(1, toObject(IntNode.valueOf(1), 
SchemaBuilder.unionOf().nullType().and().intType().endUnion()));
+    assertEquals(42.0,
+        toObject(DoubleNode.valueOf(42.0), 
SchemaBuilder.unionOf().intType().and().doubleType().endUnion()));
+    assertEquals("1", toObject(TextNode.valueOf("1"), 
SchemaBuilder.unionOf().intType().and().stringType().endUnion()));
   }
 
 }

Reply via email to