Skip to content

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

Merged
RyanSkraba merged 1 commit intoapache:mainfrom
santosh-d3vpl3x:AVRO-4225-fix-classcastexception
May 7, 2026
Merged

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

Conversation

@santosh-d3vpl3x
Copy link
Copy Markdown
Contributor

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 👍🏻

Copy link
Copy Markdown
Contributor

@RyanSkraba RyanSkraba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Reading through this looks good, it should handle both Utf8 and String when passing to the java-class constructor.

It looks like the test code comes directly from #3616, I hope it's OK to add @kevinburkesegment as Co-author (it's usually a nice thing to do even if the commits are squashed)! Thanks so much for your contributions!

@RyanSkraba RyanSkraba merged commit 974961d into apache:main May 7, 2026
9 checks passed
@RyanSkraba
Copy link
Copy Markdown
Contributor

Cherry-picked to branch-1.12.

RyanSkraba pushed a commit that referenced this pull request May 7, 2026
… 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 <kburke@twilio.com>
@santosh-d3vpl3x
Copy link
Copy Markdown
Contributor Author

It’s a great idea to have co-author assigned to @kevinburkesegment, very considerate, thanks!

@santosh-d3vpl3x santosh-d3vpl3x deleted the AVRO-4225-fix-classcastexception branch May 7, 2026 21:22
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.

3 participants