[
https://issues.apache.org/jira/browse/AVRO-4202?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Ryan Skraba resolved AVRO-4202.
-------------------------------
Fix Version/s: 1.13.0
1.12.2
Resolution: Fixed
Cherry-picked to
[branch-1.12|https://github.com/apache/avro/commit/34756d43dd247ab6ce90b67302dd9ec9b1b4b037]
It looks like this doesn't affect branch-1.11, the hashCode was introduced with
AVRO-3527
> 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
> Fix For: 1.13.0, 1.12.2
>
> 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)