Skip to content

built version isn't comparing to parsed version correctly #320

@xenoterracide

Description

@xenoterracide

parsing a v string results in objects that aren't equal

Failing test

  @Test
  void testBug() {
    var parsed = Semver.parse("v1.0.0-rc.1");
    var expected = Semver.parse("1.0.0-rc.1");
    assertThat(parsed).isEqualTo(expected);
  }
[equal] 
expected: "1.0.0-rc.1 (Semver@747b53e9)"
 but was: "1.0.0-rc.1 (Semver@7d268088)"
Expected :1.0.0-rc.1
Actual   :1.0.0-rc.1

this is the equals/hascode in Semver at the time of this writing, which feels obviously wrong. I would expect the comparison of equals to be that of the resulting semver, not the passed "originalVersion" which isn't necessarily a valid semver.

    @Override
    public boolean equals(final @Nullable Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Semver semver = (Semver) o;
        return Objects.equals(originalVersion, semver.originalVersion);
    }

    @Override
    public int hashCode() {
        return hash(originalVersion);
    }

also you shouldn't compare using getClass but instanceof for reasons. errorprone has a check on this, I think you'll find equalsverifier does too. https://errorprone.info/bugpattern/EqualsGetClass could add this check as an error after merging #317

Metadata

Metadata

Assignees

No one assigned

    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