Skip to content

bug: RecordType.hashCode() violates equals/hashCode contract #694

@mattpapp

Description

@mattpapp

Hi team,

I was going through the code to get familiar and found a bug in RecordType.hashCode().

The equals() method uses Arrays.equals() to compare field names by content but hashCode() passes the fieldNames array directly to Objects.hash() which ends up using the array's identity hash instead of its content. So basically two RecordType instances with the same field names are considered equal but will give different hashcodes which is wrong.

I wrote a small temp test locally as a sanity check:

RecordType rt1 = new RecordType("x", "y", "z");
RecordType rt2 = new RecordType("x", "y", "z");

rt1.equals(rt2)  //true
rt1.hashCode() == rt2.hashCode()  //false

HashMap<RecordType, String> map = new HashMap<>();
map.put(rt1, "z");
map.get(rt2); //this will return null instead of "z"

The fix is pretty simple: line 68 in RecordType.java just needs to wrap the array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions