Skip to content

Fix RefToken equals/hashCode to include semantic identity (index and keyRef fields) #223

@coderabbitai

Description

@coderabbitai

Description

The RefToken class has a semantic bug in its equals() and hashCode() implementations. Currently, these methods only consider the decodedToken field, ignoring the index and keyRef fields. This causes semantically different tokens to be incorrectly treated as equal.

Problem

Two RefToken instances can have the same decodedToken but represent different operations:

  • append("0") creates RefToken("0", null, null) — field access
  • append(0) creates RefToken("0", 0, null) — array index access

The current implementation would incorrectly consider these tokens equal, which can cause AbstractJsonPointer.equals() to fail when comparing paths that differ in semantic type (e.g., /a/0/b where the 0 is a field name vs. an array index).

Expected Behavior

The equals() method should compare all three fields: decodedToken, index, and keyRef using null-safe comparisons (e.g., Objects.equals).

The hashCode() method should include all three fields (e.g., Objects.hash(decodedToken, index, keyRef)).

Additional Context

This is a pre-existing issue from the original JsonPointer implementation where RefToken was an inner class. It was identified during PR #221 (Jackson 3.x support) when RefToken was extracted to a standalone class.

Reference:

Reported by: @sondemar

Location

src/main/java/com/flipkart/zjsonpatch/RefToken.java lines 121-134

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