Skip to content

AVRO-4225: Fix ClassCastException in FastReaderBuilder for java-class attribute#3715

Open
santosh-d3vpl3x wants to merge 1 commit intoapache:mainfrom
santosh-d3vpl3x:AVRO-4225-fix-classcastexception
Open

AVRO-4225: Fix ClassCastException in FastReaderBuilder for java-class attribute#3715
santosh-d3vpl3x wants to merge 1 commit intoapache:mainfrom
santosh-d3vpl3x:AVRO-4225-fix-classcastexception

Conversation

@santosh-d3vpl3x
Copy link
Copy Markdown

What is the purpose of the change

Fixes AVRO-4225: When using GenericDatumReader with schemas containing java-class attributes on string fields (e.g., java.math.BigDecimal), FastReaderBuilder throws:

ClassCastException: Utf8 cannot be cast to String

The bug is in getTransformingStringReader() which casts the result of stringReader.read() directly to String, but GenericData returns Utf8.

The Fix

Explicitly handle both Utf8 and String types, consistent with the rest of the Avro codebase:

Object value = stringReader.read(null, decoder);
String stringValue = value instanceof Utf8 ? ((Utf8) value).toString() : (String) value;
return transformer.apply(stringValue);

Verifying this change

Added test class FastReaderBuilderJavaClassTest with two test cases:

  • genericDatumReaderWithJavaClassAttribute - tests union type with java-class
  • genericDatumReaderWithDirectJavaClassString - tests direct string field with java-class

Both tests fail without the fix (ClassCastException) and pass with the fix.

Documentation

  • Does this pull request introduce a new feature? No
  • If yes, how is the feature documented? N/A

… attribute

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.
@github-actions github-actions bot added the Java Pull Requests for Java binding label Mar 31, 2026
@gabru-md
Copy link
Copy Markdown

this will be useful 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Pull Requests for Java binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants