You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by "Etienne Hardy (Jira)" <ji...@apache.org> on 2023/08/21 12:29:00 UTC

[jira] [Comment Edited] (AVRO-3161) bad classcast, avro-maven-plugin not respecing configured stringType for collections

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

Etienne Hardy edited comment on AVRO-3161 at 8/21/23 12:28 PM:
---------------------------------------------------------------

The ClassCastException does not happen in the generated put() method where such a cast like above is done, but rather in client code when retrieving objects from the list that are supposed to be java.lang.String.

 

Here's a GitHub repo with the test case to reproduce the problem: https://github.com/ehardy/avro-class-cast-exception


was (Author: JIRAUSER301814):
The ClassCastException does not happen in the generated put() method where such a cast like above is done, but rather in client code when retrieving objects from the list that are supposed to be java.lang.String. I'm trying to attach my reproducible test case to this comment but somehow the zip file never seems to get attached.

> bad classcast, avro-maven-plugin not respecing configured stringType for collections
> ------------------------------------------------------------------------------------
>
>                 Key: AVRO-3161
>                 URL: https://issues.apache.org/jira/browse/AVRO-3161
>             Project: Apache Avro
>          Issue Type: Bug
>    Affects Versions: 1.9.2
>            Reporter: Martin Mucha
>            Priority: Major
>
> Using avro schema defining element as:
> {code:java}
> {
>  "name": "field1",
>  "type": "string"
> },{code}
> and 
> {code:java}
> {
>  "name": "field2",
>  "type": ["null", {
>  "type": "array",
>  "name": "field2Array",
>  "items": {
>  "type": "string"
>  }
>  }],
>  "default": null
> }{code}
>  
> the avro-maven-plugin will generate put method, which will look like this:
>  
> {code:java}
> public void put(int field$, java.lang.Object value$) {
>  switch (field$) {
>  case 1: field1 = value$ != null ? value$.toString() : null; break;
>  case 19: field2 = (java.util.List<java.lang.String>)value$; break;
>  default: throw new org.apache.avro.AvroRuntimeException("Bad index");
>  }
>  }{code}
>  
> the problem is, that `value$.toString()` will correctly turn Utf8 to String, while unchecked cast of List<Utf8> to List<String> will successfully trick the compiller, but the items will still be of type Utf8.
> Plugin configuration:
> {code:java}
> <plugin>
>  <groupId>org.apache.avro</groupId>
>  <artifactId>avro-maven-plugin</artifactId>
>  <version>1.9.2</version>
>  <executions>
>  <execution>
>  <id>generateClassesFromTestSchemata</id>
>  <phase>generate-sources</phase>
>  <goals>
>  <goal>schema</goal>
>  </goals>
>  <configuration>
>  <stringType>String</stringType>
>  <fieldVisibility>PRIVATE</fieldVisibility>
>  <testSourceDirectory>...</testSourceDirectory>
>  <testOutputDirectory>...</testOutputDirectory>
>  </configuration>
>  </execution>
>  </executions>
> </plugin>{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)