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

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


The following commit(s) were added to refs/heads/branch-1.12 by this push:
     new 7c9b3dd32d AVRO-4225: Fix ClassCastException in FastReaderBuilder for 
java-class attribute (#3715)
7c9b3dd32d is described below

commit 7c9b3dd32d5f3fd79b66fe439515608c9b686fff
Author: Santosh Pingale <[email protected]>
AuthorDate: Thu May 7 19:22:05 2026 +0200

    AVRO-4225: Fix ClassCastException in FastReaderBuilder for java-class 
attribute (#3715)
    
    When using GenericDatumReader with schemas containing java-class
    attributes on string fields, FastReaderBuilder.getTransformingStringReader()
    was casting stringReader.read() directly to String, but GenericData
    returns Utf8, causing ClassCastException.
    
    Fix by explicitly handling both Utf8 and String types, consistent with
    the rest of the Avro codebase.
    
    Co-authored-by: Kevin Burke <[email protected]>
---
 .../java/org/apache/avro/io/FastReaderBuilder.java |   6 +-
 .../avro/io/FastReaderBuilderJavaClassTest.java    | 116 +++++++++++++++++++++
 2 files changed, 121 insertions(+), 1 deletion(-)

diff --git 
a/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java 
b/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
index 7ff80e2a4f..7bb66a7936 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
@@ -438,7 +438,11 @@ public class FastReaderBuilder {
       Function<String, ?> transformer = findClass(valueClass)
           .map(clazz -> ReflectionUtil.getConstructorAsFunction(String.class, 
clazz)).orElse(null);
       if (transformer != null) {
-        return (old, decoder) -> transformer.apply((String) 
stringReader.read(null, decoder));
+        return (old, decoder) -> {
+          Object value = stringReader.read(null, decoder);
+          String stringValue = value instanceof Utf8 ? ((Utf8) 
value).toString() : (String) value;
+          return transformer.apply(stringValue);
+        };
       }
     }
 
diff --git 
a/lang/java/avro/src/test/java/org/apache/avro/io/FastReaderBuilderJavaClassTest.java
 
b/lang/java/avro/src/test/java/org/apache/avro/io/FastReaderBuilderJavaClassTest.java
new file mode 100644
index 0000000000..8a23133366
--- /dev/null
+++ 
b/lang/java/avro/src/test/java/org/apache/avro/io/FastReaderBuilderJavaClassTest.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.io;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericDatumReader;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for FastReaderBuilder behavior with schemas containing "java-class"
+ * attributes.
+ */
+public class FastReaderBuilderJavaClassTest {
+
+  /**
+   * Tests that GenericDatumReader can deserialize records with string fields 
that
+   * have a "java-class" attribute (e.g., BigDecimal).
+   *
+   * This test reproduces a bug where
+   * FastReaderBuilder.getTransformingStringReader() casts the result of
+   * stringReader.read() directly to String, but in GenericData mode the reader
+   * returns Utf8, causing a ClassCastException.
+   */
+  @Test
+  void genericDatumReaderWithJavaClassAttribute() throws IOException {
+    // Schema with a string field that has "java-class": "java.math.BigDecimal"
+    // This is a common pattern for representing decimal values as strings
+    String schemaJson = "{\n" + "  \"type\": \"record\",\n" + "  \"name\": 
\"TestRecord\",\n" + "  \"fields\": [\n"
+        + "    {\"name\": \"id\", \"type\": \"string\"},\n" + "    {\"name\": 
\"price\", \"type\": [\"null\", {\n"
+        + "      \"type\": \"string\",\n" + "      \"java-class\": 
\"java.math.BigDecimal\"\n" + "    }]}\n" + "  ]\n"
+        + "}";
+
+    Schema schema = new Schema.Parser().parse(schemaJson);
+
+    GenericRecord record = new GenericData.Record(schema);
+    record.put("id", "123");
+    record.put("price", "-0.0002");
+
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    GenericDatumWriter<GenericRecord> writer = new 
GenericDatumWriter<>(schema);
+    BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
+    writer.write(record, encoder);
+    encoder.flush();
+
+    byte[] serialized = out.toByteArray();
+
+    // Deserialize using GenericDatumReader (which uses FastReaderBuilder by
+    // default)
+    GenericDatumReader<GenericRecord> reader = new 
GenericDatumReader<>(schema);
+    BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(serialized, 
null);
+
+    // AVRO-4225 this should not throw ClassCastException: Utf8 cannot be cast
+    // to String
+    GenericRecord result = reader.read(null, decoder);
+
+    assertNotNull(result);
+    assertEquals("123", result.get("id").toString());
+    assertEquals("-0.0002", result.get("price").toString());
+  }
+
+  /**
+   * Tests that GenericDatumReader can deserialize records with a direct string
+   * field (not in a union) that has a "java-class" attribute.
+   */
+  @Test
+  void genericDatumReaderWithDirectJavaClassString() throws IOException {
+    String schemaJson = "{\n" + "  \"type\": \"record\",\n" + "  \"name\": 
\"TestRecord\",\n" + "  \"fields\": [\n"
+        + "    {\"name\": \"amount\", \"type\": {\n" + "      \"type\": 
\"string\",\n"
+        + "      \"java-class\": \"java.math.BigDecimal\"\n" + "    }}\n" + "  
]\n" + "}";
+
+    Schema schema = new Schema.Parser().parse(schemaJson);
+
+    GenericRecord record = new GenericData.Record(schema);
+    record.put("amount", "123.45");
+
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    GenericDatumWriter<GenericRecord> writer = new 
GenericDatumWriter<>(schema);
+    BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);
+    writer.write(record, encoder);
+    encoder.flush();
+
+    byte[] serialized = out.toByteArray();
+
+    GenericDatumReader<GenericRecord> reader = new 
GenericDatumReader<>(schema);
+    BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(serialized, 
null);
+
+    GenericRecord result = reader.read(null, decoder);
+
+    assertNotNull(result);
+    assertEquals("123.45", result.get("amount").toString());
+  }
+}

Reply via email to