semver4j icon indicating copy to clipboard operation
semver4j copied to clipboard

Semver.isEquivalentTo() returns always false for larger major versions and SemverType.NPM

Open oheger-bosch opened this issue 3 years ago • 1 comments

For my use case I have to deal with version numbers that use Calendar versioning. Such version numbers have the same structure as semantic version numbers, although the single components cannot be assigned to patch, minor, or major levels. Nevertheless, it should be possible to check whether such a version number is within a specific range.

I found out, however, that those checks always fail. I narrowed down the problem to a minimum failing test case:

Semver v1 = new Semver("2021.1.6", Semver.SemverType.NPM);
Semver v2 = new Semver("2021.1.6", Semver.SemverType.NPM);

assertTrue(v1.isEquivalentTo(v2));  // Fails

The cause for the problem seems to be in the isEqualTo() method. Here the major version numbers (which are Integer objects) are compared to using != (i.e. reference semantics). As the Integer instances will be different, the major versions are always considered as not equivalent.

The comparison works for smaller numbers because Integer.valueOf() has a cache for instances within a specific range. So in this range, the comparison of references yields correct results, but in general, the equals() method should be used.

oheger-bosch avatar Mar 08 '21 10:03 oheger-bosch

@oheger-bosch if you are still interesting, I've made copy of this lib and fix bug reported by you. Look for version 2.0.1

piotrooo avatar Jul 22 '22 22:07 piotrooo