Skip to content

MutableContext / MutableStructure equality bug #1362

@glissand0

Description

@glissand0

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?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions