You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by "ASF subversion and git services (Jira)" <ji...@apache.org> on 2026/05/07 17:23:00 UTC
[jira] [Commented] (AVRO-4225) GenericDatumReader throws ClassCastException for schemas with "java-class" attribute on string fields
[ https://issues.apache.org/jira/browse/AVRO-4225?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18079206#comment-18079206 ]
ASF subversion and git services commented on AVRO-4225:
-------------------------------------------------------
Commit 974961d97a84d3601113d0f3cdc41db6580d1b6b in avro's branch refs/heads/main from Santosh Pingale
[ https://gitbox.apache.org/repos/asf?p=avro.git;h=974961d97a ]
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 <kb...@twilio.com>
> GenericDatumReader throws ClassCastException for schemas with "java-class" attribute on string fields
> -----------------------------------------------------------------------------------------------------
>
> Key: AVRO-4225
> URL: https://issues.apache.org/jira/browse/AVRO-4225
> Project: Apache Avro
> Issue Type: Bug
> Affects Versions: 1.12.1
> Reporter: Kevin Burke
> Priority: Minor
> Labels: pull-request-available
> Time Spent: 0.5h
> Remaining Estimate: 0h
>
> A minimal reproduction test case can be found here: https://github.com/apache/avro/pull/3616
> When deserializing records using {{GenericDatumReader}} with schemas that have a {{"java-class"}} attribute on string fields (e.g., {{{}"java-class": "java.math.BigDecimal"{}}}), Avro throws a {{{}ClassCastException{}}}.
> *Schema example:*
>
> {{{
> "type": "record",
> "name": "TestRecord",
> "fields": [
> \{"name": "price", "type": ["null", {
> "type": "string",
> "java-class": "java.math.BigDecimal"
> }]}
> ]
> }}}
> *Stack trace:*
>
> {{java.lang.ClassCastException: class org.apache.avro.util.Utf8 cannot be cast to class java.lang.String
> at org.apache.avro.io.FastReaderBuilder.lambda$getTransformingStringReader$32(FastReaderBuilder.java:440)
> at org.apache.avro.io.FastReaderBuilder.lambda$createUnionReader$30(FastReaderBuilder.java:412)
> at org.apache.avro.io.FastReaderBuilder.lambda$createFieldSetter$1(FastReaderBuilder.java:181)
> at org.apache.avro.io.FastReaderBuilder$RecordReader.read(FastReaderBuilder.java:575)
> at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:150)}}
> *Root cause:*
> In {{FastReaderBuilder.getTransformingStringReader()}} (line 440), the code casts the result of {{stringReader.read()}} directly to {{{}String{}}}:
>
> {{return (old, decoder) -> transformer.apply((String) stringReader.read(null, decoder));}}
> However, when using {{{}GenericData{}}}, the string reader returns {{{}Utf8{}}}, not {{{}String{}}}. This causes the {{{}ClassCastException{}}}.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)