Skip to content

Version.compareTo Returns -18 for "alpha" vs "snapshot" #1432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ILikeProgramminghhh opened this issue May 19, 2025 · 2 comments
Open

Version.compareTo Returns -18 for "alpha" vs "snapshot" #1432

ILikeProgramminghhh opened this issue May 19, 2025 · 2 comments

Comments

@ILikeProgramminghhh
Copy link

Hi,

I came across a case where I was comparing two Version objects like this:

Version version1 = new Version(1, 0, 0, "alpha");
Version version2 = new Version(1, 0, 0, "snapshot");
int result = version1.compareTo(version2);
System.out.println(result); // returns -18

I was a bit confused by the result -18, which comes from a lexicographical comparison of the snapshot info strings. I had initially expected "alpha" to be considered newer than "snapshot", or for the comparison to return a normalized result like -1, 0, or 1.

I’d like to ask:

  • Is this the intended behavior, or could it be considered a bug?

  • Is the snapshot info string always compared lexicographically?

  • Should users expect comparison results beyond just -1/0/1?

  • Is there any semantic ordering (e.g., "alpha" < "beta" < "rc" < "snapshot"), or is this left entirely to user interpretation?

Clarifying this in the Javadoc or documentation would help prevent confusion for users relying on version comparison behavior.

Thanks!

@pjfanning
Copy link
Member

pjfanning commented May 19, 2025

This is an internal class. It is not part of the public Jackson API. Jackson is a lib for parsing and generating JSON. It is not a lib for comparing version strings. There are libs that specialise in that sort of thing. Semver4J is one.

@cowtowncoder
Copy link
Member

Right, maybe Version should not even be Comparable, but the intent definitely is for it to only be used for Jackson components and extension Modules. Latter can be from external developers.
But the main intent is for visibility, and (to a degree) to allow for compatibility comparison wrt version numbers. Classifier String ordering is not really defined, esp. wrt SNAPSHOT vs alpha.

Note on numeric value: I don't think compareTo() is expected to normalized value: they are to be only used for "is it 0, less than 0 or greater than 0" comparison.
With that, I think that:

  • Is this the intended behavior, or could it be considered a bug? Not a bug
  • Is the snapshot info string always compared lexicographically? Yes, String qualifier/classifier part is to be sorted lexicographically (without other semantics)
  • Should users expect comparison results beyond just -1/0/1? Users should not assume only -1/0/1, so yet
  • Is there any semantic ordering (e.g., "alpha" < "beta" < "rc" < "snapshot"), or is this left entirely to user interpretation? No semantic ordering -- Maven has utility classes for users who'd like to get useful semantic ordering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants