Skip to content

[MNG-7594] ComparableVersion#getCanonical() returns incorrect results for zero between hyphens #8873

Open

Description

@jira-importer

Niklas Matthies opened MNG-7594 and commented

The getCanonical() method incorrectly reduces version strings like "1-0-1" to "1-1". This leads to contradictory results like in:

$ java -jar /path/to/maven-artifact-3.8.x.jar 1-0-1 1-1
1. 1-0-1 -> 1-1; tokens: [1, [[1]]]
   1-0-1 < 1-1
2. 1-1 -> 1-1; tokens: [1, [1]]

Here 1. and 2. imply that both versions should be equal, but they still compare unequal (1-0-1 < 1-1). Arguably, the correct canonicalization for 1-0-1 here should be 1--1 (two hyphens), since 1--1 compares equal to 1-0-1.

This is related to Bug 2 of MNG-6420, but could probably be fixed separately. I.e. just fix the broken canonicalization to make it consistent with compareTo(), but keep the comparison unchanged, without having to wait for a new spec to be written.

Note: This assumes that compareTo() is well-behaved for equality, i.e. defines a sound equivalence relation, which I think it does. It doesn't define a correct total order, as issues like MNG-6568 demonstrate, but that isn't relevant for making getCanonical() consistent with compareTo().


Affects: 3.8.6

Activity

  1. jira-importer commented on Nov 8, 2022

    @jira-importer
    Author

    Michael Osipov commented

    The more I read such issues there more I think that we need to apply strict semantics versioning becasue there will be always cases with a lenient format which will fail. :-(

  2. added
    bug
    priority:major
    on Jul 7, 2025
  3. Tarun4201 commented on Oct 12, 2026

    @Tarun4201
    Contributor

    [MNG-7594] Fix ComparableVersion getCanonical for zero between hyphens

    Summary

    Resolves the issue where version strings like 1-0-1 had contradictory canonicalization and comparison results. Previously, ListItem.normalize() removed intermediate zero elements before child lists, resulting in 1-0-1 being canonicalized to 1-1 while still sorting as 1-0-1 < 1-1.

    Changes

    1. ListItem.normalize():
      • Added safe pattern matching and non-empty guards when inspecting following ListItem entries before determining if preceding null/zero items can be pruned.
    2. Regression Tests (ComparableVersionTest#testMng7594):
      • Verified that 1-0-1 canonicalizes to 1-0-1 and compares equal to 1--1.
      • Verified 1-0-1 < 1-1 and canonical("1-0-1") != canonical("1-1").
      • Covered related zero-between-hyphen cases (e.g. 1-0-2 < 1-0.1 < 1-2 from MNG-6420, multiple hyphenated zeros 1-0-0-1, and normalized trailing zeros like 1-0-1-0 == 1-0-1).
  4. arturobernalg commented on Oct 16, 2026

    @arturobernalg
    Member

    This is already fixed on master and on maven-4.0.x: 1-0-1 now canonicalizes to 1-0-1, compares equal to 1--1 and below 1-1. The fix came with #13102 (MNG-6568), and #13441 added the regression test (ComparableVersionTest#testMng7594).

    It is not fixed on maven-3.10.x, where 1-0-1 still canonicalizes to 1-1. The 3.x ComparableVersion is a different implementation, so #13102 cannot be backported as is. It also changes the ordering that 3.x has always had: on 3.10.x 1.0-1 equals 1-1, on master it is greater. Fixing only the canonical form on 3.10.x would hit the same rule, so I did not open a PR for it. Maintainers, is a change in 3.10.x wanted here? Otherwise this issue can be closed as fixed in 4.x.

  5. added a commit that references this issue on Oct 20, 2026
    8c9d57a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority:major

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions