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

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

Commit 34756d43dd247ab6ce90b67302dd9ec9b1b4b037 in avro's branch 
refs/heads/branch-1.12 from Steven Aerts
[ https://gitbox.apache.org/repos/asf?p=avro.git;h=34756d43dd ]

AVRO-4202: Refer to fields for generated hashCode (#3547)

The generated hashCode function has a local parameter called result.
To prevent this local variable to conflict with field which have the
same name, it has to refer to the fields with `this.<fieldName>` making
it unambiguous refer to fields and not local variables.

> Avro tools hashCode method conflict with schema field result
> ------------------------------------------------------------
>
>                 Key: AVRO-4202
>                 URL: https://issues.apache.org/jira/browse/AVRO-4202
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.12.1
>            Reporter: Ben Canton
>            Priority: Minor
>              Labels: pull-request-available
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> There is a bug in the Avro tools 1.12.1 following this PR 
> [https://github.com/apache/avro/pull/1708#issuecomment-3523836333] when a 
> schema includes a field with name {{{}result{}}}.
> In the following example, the schema contains two fields:
>  * A field {{result}} of type {{["null", "string"]}}
>  * A field {{anotherField}} of type` enum
> The {{hashCode}} method generated based on the {{record.vm}} template will be 
> as follow:
>   @Overridepublic int hashCode() {    int result = 1;    result = 31 * result 
> + (eventHeader == null ? 0 : eventHeader.hashCode());    result = 31 * result 
> + (result == null ? 0 : result.hashCode());    result = 31 * result + 
> (anotherField == null ? 0 : ((java.lang.Enum) anotherField).ordinal());    
> return result;
>   }
>  
> Now since the schema has a field {{result}} of type {{["null", "string"]}} 
> and the generated {{hashCode}} method declares a {{result}} of type 
> {{{}int{}}}. The {{result.hashCode()}} will fail since the local {{result}} 
> takes precedence.
> Trying to compile a project using the generated java class throws:
> {code:java}
> Compiling with JDK Java compiler API. ..../MyEvent.java:1744: error: bad 
> operand types for binary operator '==' result = 31 * result + (result == null 
> ? 0 : result.hashCode()); ^ first type: int second type: <null> 
> ..../MyEvent.java:1744: error: int cannot be dereferenced result = 31 * 
> result + (result == null ? 0 : result.hashCode()); ^ 2 errors > Task 
> :compileJava FAILED{code}
> For the sake of validating my findings, I have updated my schema and renamed 
> my field {{result}} to {{res}} and sure enough that "fixed" the issue. 
> Obviously, this is not a solution.
> One workaround would be to use custom Velocity templates, to rename {{int 
> result = 1}} to {{int result2 = 1}} (just an example) but that is unpractical 
> to maintain.
> It would be great if we could rename the {{result}} in the {{record.vm}} 
> template to be something less common.



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

Reply via email to