-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix version comparison logic in update checker #4466
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,34 @@ private static class VersionJsonTemplate implements JsonTemplate { | |
| * @param log {@link Consumer} used to log messages | ||
| * @return a new {@link UpdateChecker} | ||
| */ | ||
|
|
||
| @VisibleForTesting | ||
| static int compareVersions(String v1, String v2) { | ||
| String[] p1 = v1.split("\\."); | ||
| String[] p2 = v2.split("\\."); | ||
|
|
||
| int len = Math.max(p1.length, p2.length); | ||
| for (int i = 0; i < len; i++) { | ||
| int a = i < p1.length ? parsePart(p1[i]) : 0; | ||
| int b = i < p2.length ? parsePart(p2[i]) : 0; | ||
| if (a != b) { | ||
| return Integer.compare(a, b); | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| private static int parsePart(String s) { | ||
| try { | ||
| // Extract leading numeric portion (e.g., "1-SNAPSHOT" → "1") | ||
| String digits = s.replaceAll("^(\\d+).*$", "$1"); | ||
| return Integer.parseInt(digits); | ||
| } catch (NumberFormatException e) { | ||
| return 0; | ||
| } | ||
| } | ||
|
||
|
|
||
|
|
||
| public static Future<Optional<String>> checkForUpdate( | ||
| ExecutorService executorService, | ||
| String versionUrl, | ||
|
|
@@ -118,10 +146,16 @@ static Optional<String> performUpdateCheck( | |
| Files.write(lastUpdateCheckTemp, Instant.now().toString().getBytes(StandardCharsets.UTF_8)); | ||
| Files.move(lastUpdateCheckTemp, lastUpdateCheck, StandardCopyOption.REPLACE_EXISTING); | ||
|
|
||
| if (currentVersion.equals(version.latest)) { | ||
| if (version.latest == null || version.latest.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(version.latest); | ||
|
|
||
| if (compareVersions(currentVersion, version.latest) < 0) { | ||
| return Optional.of(version.latest); // only report if newer | ||
| } | ||
|
|
||
| return Optional.empty(); // otherwise equal or older | ||
|
|
||
| } finally { | ||
| httpClient.shutDown(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation correctly handles numeric version parts but does not correctly handle pre-release identifiers like
-alphaor-SNAPSHOT. For example,1.0.0-SNAPSHOTshould be considered an older version than1.0.0, but this implementation will treat them as equal. This could lead to missed update notifications for users on pre-release versions.I recommend adding test cases to cover these scenarios, for example:
To support this, the
compareVersionsmethod would need to be updated to handle these suffixes. One approach is to check for a hyphen in version parts. If the numeric parts are equal, the version part with a suffix can be considered smaller than one without. If both have suffixes, they can be compared lexicographically.