Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MutableContext / MutableStructure equality bug #1362

Open
glissand0 opened this issue Mar 6, 2025 · 0 comments · May be fixed by #1364
Open

MutableContext / MutableStructure equality bug #1362

glissand0 opened this issue Mar 6, 2025 · 0 comments · May be fixed by #1364
Assignees
Labels
bug Something isn't working

Comments

@glissand0
Copy link

MutableContext stores a MutableStructure instance, which uses Lombok's @EqualsAndHashCode. Using IntelliJ's delombok, this is the generated code

public boolean equals(final Object o) {
      if (o == this) {
          return true;
      }
      if (!(o instanceof MutableStructure)) {
          return false;
      }
      final MutableStructure other = (MutableStructure) o;
      if (!other.canEqual((Object) this)) {
          return false;
      }
      return true;
  }

  protected boolean canEqual(final Object other) {return other instanceof MutableStructure;}

  public int hashCode() {
      int result = 1;
      return result;
  }

ie, all MutableStructure objects will be treated as equal and the below tests pass

    @Test
    void mutableStructureEquality() {
        MutableStructure m1 = new MutableStructure();
        m1.add("key1", "val1");
        MutableStructure m2 = new MutableStructure();
        m1.add("key2", "val2");
        assertEquals(m1, m2);
    }

    @Test
    void mutableCtxEquality() {
        final Map<String, Value> attributes = new HashMap<>();
        attributes.put("key1", new Value("val1"));
        final MutableContext ctx = new MutableContext(attributes);

        final Map<String, Value> attributes2 = new HashMap<>();
        final MutableContext ctx2 = new MutableContext(attributes2);

        assertThat(ctx).isEqualTo(ctx2);
    }

This is Lombok's behaviour when a class does not contain any attributes, regardless of what is in the parent class (see, eg https://medium.com/vena-engineering/the-inheritance-hashset-related-bug-with-lombok-36dbcfb04381)

This can be fixed by changing the equality annotation on MutableStructure to @EqualsAndHashCode(callSuper = true)

On a related note, ImmutableContext does not override equals at all. This could be the intention, but I think it would be useful if it did?

@chrfwow chrfwow added Needs Triage This issue needs to be investigated by a maintainer bug Something isn't working and removed Needs Triage This issue needs to be investigated by a maintainer labels Mar 6, 2025
@chrfwow chrfwow linked a pull request Mar 6, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants