Skip to content

Commit 70b7fa3

Browse files
committed
fix: handle NumberFormatException in version parsing
Add try-catch blocks to handle potential NumberFormatException when parsing version numbers from index names and version strings. - IndexMigrationService.getCurrentIndexVersion(): wrap Integer.parseInt with try-catch and log warning on failure, returning 0 as default - VersionService.getNextVersion(): wrap all three Integer.parseInt calls with try-catch blocks, using sensible defaults (1.0.0)
1 parent 106cb61 commit 70b7fa3

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

redis-om-spring/src/main/java/com/redis/om/spring/indexing/IndexMigrationService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,12 @@ public int getCurrentIndexVersion(Class<?> entityClass) {
318318
Matcher matcher = VERSION_PATTERN.matcher(currentIndexName);
319319

320320
if (matcher.find()) {
321-
return Integer.parseInt(matcher.group(1));
321+
try {
322+
return Integer.parseInt(matcher.group(1));
323+
} catch (NumberFormatException e) {
324+
logger.warn("Failed to parse index version from name '{}': {}", currentIndexName, matcher.group(1));
325+
return 0;
326+
}
322327
}
323328

324329
return 0; // No version found, assume version 0

tests/src/test/java/com/redis/om/spring/services/VersionService.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,27 @@ public boolean isMajorVersion() {
7171
* @return the next version string
7272
*/
7373
public String getNextVersion(String incrementType) {
74-
int major = Integer.parseInt(getMajorVersion());
75-
int minor = Integer.parseInt(getMinorVersion());
76-
int patch = Integer.parseInt(getPatchVersion());
74+
int major;
75+
int minor;
76+
int patch;
77+
78+
try {
79+
major = Integer.parseInt(getMajorVersion());
80+
} catch (NumberFormatException e) {
81+
major = 1;
82+
}
83+
84+
try {
85+
minor = Integer.parseInt(getMinorVersion());
86+
} catch (NumberFormatException e) {
87+
minor = 0;
88+
}
89+
90+
try {
91+
patch = Integer.parseInt(getPatchVersion());
92+
} catch (NumberFormatException e) {
93+
patch = 0;
94+
}
7795

7896
switch (incrementType.toLowerCase()) {
7997
case "major":

0 commit comments

Comments
 (0)