Skip to content
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

Fix NumberInput.looksLikeValidNumber() implementation #1241

Merged
merged 6 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/io/NumberInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class NumberInput
* @since 2.17
*/
private final static Pattern PATTERN_FLOAT = Pattern.compile(
"[+-]?[0-9]+(\\.[0-9]+)?([eE][+-]?[0-9]+)?");
"[+-]?([0-9]+)?(\\.[0-9]+)?([eE][+-]?[0-9]+)?");

/**
* Fast method for parsing unsigned integers that are known to fit into
Expand Down Expand Up @@ -578,6 +578,7 @@ public static BigInteger parseBigIntegerWithRadix(final String s, final int radi
* @since 2.17
*/
public static boolean looksLikeValidNumber(final String s) {
return (s != null) && PATTERN_FLOAT.matcher(s).matches();
// the PATTERN_FLOAT match returns true for empty strings, so we need to check for digits separately
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
return s != null && PATTERN_FLOAT.matcher(s).matches() && s.chars().anyMatch(Character::isDigit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public void testLooksLikeValidNumber()
assertTrue(NumberInput.looksLikeValidNumber("-1"));
assertTrue(NumberInput.looksLikeValidNumber("0001")); // non-JSON

// https://github.com/FasterXML/jackson-databind/issues/4435
assertTrue(NumberInput.looksLikeValidNumber(".01"));
assertTrue(NumberInput.looksLikeValidNumber("+.01"));
assertTrue(NumberInput.looksLikeValidNumber("-.01"));

assertTrue(NumberInput.looksLikeValidNumber("0.01"));
assertTrue(NumberInput.looksLikeValidNumber("-0.10"));
assertTrue(NumberInput.looksLikeValidNumber("+0.25")); // non-JSON
Expand All @@ -84,6 +89,7 @@ public void testLooksLikeValidNumber()

assertFalse(NumberInput.looksLikeValidNumber(""));
assertFalse(NumberInput.looksLikeValidNumber(" "));
assertFalse(NumberInput.looksLikeValidNumber("."));
assertFalse(NumberInput.looksLikeValidNumber("10_000"));
}
}