jsemver icon indicating copy to clipboard operation
jsemver copied to clipboard

MetadataVersion is not immutable

Open janjoerke opened this issue 5 years ago • 2 comments

When executing the increment method of MetadataVersion, the original and the returned MetadataVersion share the same idents array, as no new array is created for the new MetadataVersion object.

MetadataVersion increment() {
    String[] ids = idents;
    String lastId = ids[ids.length - 1];
    if (isInt(lastId)) {
        int intId = Integer.parseInt(lastId);
        ids[ids.length - 1] = String.valueOf(++intId); // modifies original
    } else {
        ids = Arrays.copyOf(ids, ids.length + 1);
        ids[ids.length - 1] = String.valueOf(1);
    }
    return new MetadataVersion(ids);
}

The test case does not reveal this error, because it only verifies that the original MetadataVersion and the returned MetadataVersion do not reference the same object.

@Test
public void shouldBeImmutable() {
    MetadataVersion v1 = new MetadataVersion(
        new String[] {"alpha", "1"}
    );
    MetadataVersion v2 = v1.increment();
    assertNotSame(v1, v2); // only checks objects references
}

janjoerke avatar May 24 '19 07:05 janjoerke

The shouldBeImmutable() test method in VersionTest has also some problems connected to assertNotSame.

janjoerke avatar May 24 '19 09:05 janjoerke

I don't think this library is maintained any more. You may want to check out https://github.com/asarkar/jsemver.

asarkar avatar Nov 09 '20 07:11 asarkar

Hello Jan! Sorry for the delay and thank you for your contribution!

zafarkhaja avatar Apr 12 '23 14:04 zafarkhaja