[ 
https://issues.apache.org/jira/browse/AVRO-4225?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18079210#comment-18079210
 ] 

ASF subversion and git services commented on AVRO-4225:
-------------------------------------------------------

Commit 7c9b3dd32d5f3fd79b66fe439515608c9b686fff in avro's branch 
refs/heads/branch-1.12 from Santosh Pingale
[ https://gitbox.apache.org/repos/asf?p=avro.git;h=7c9b3dd32d ]

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]>

> 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
>             Fix For: 1.13.0, 1.12.2
>
>          Time Spent: 40m
>  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)

Reply via email to