Skip to content

Commit

Permalink
fix(FasterXML#291): InstantDeserializer fail parse neg num
Browse files Browse the repository at this point in the history
Full Issue Title: InstantDeserializer fails to parse negative numeric
timestamp strings for pre-1970 values.

- Updated `InstantDeserializer._countPeriods(String)` to check for `-`
  and `+` at beginning of numeric string.
- Additionally, added check if `ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS` is
  enabled. If disabled, throws same exception as before change from
  `_countPeriods(...)`.
- Moved associated tests out of `failing` directory
  • Loading branch information
Strongbeard committed Mar 3, 2025
1 parent e02ab87 commit 45725b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.fasterxml.jackson.core.json.JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS;

/**
* Deserializer for Java 8 temporal {@link Instant}s, {@link OffsetDateTime},
* and {@link ZonedDateTime}s.
Expand Down Expand Up @@ -380,11 +382,16 @@ protected boolean shouldReadTimestampsAsNanoseconds(DeserializationContext conte
}

// Helper method to find Strings of form "all digits" and "digits-comma-digits"
protected int _countPeriods(String str)
protected int _countPeriods(JsonParser p, String str)
{
int commas = 0;
for (int i = 0, end = str.length(); i < end; ++i) {
int ch = str.charAt(i);
int i = 0;
int ch = str.charAt(i);
if (ch == '-' || (ch == '+' && p.isEnabled(ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS.mappedFeature()))) {
++i;
}
for (int end = str.length(); i < end; ++i) {
ch = str.charAt(i);
if (ch < '0' || ch > '9') {
if (ch == '.') {
++commas;
Expand Down Expand Up @@ -413,7 +420,7 @@ protected T _fromString(JsonParser p, DeserializationContext ctxt,
_formatter == DateTimeFormatter.ISO_ZONED_DATE_TIME
) {
// 22-Jan-2016, [datatype-jsr310#16]: Allow quoted numbers too
int dots = _countPeriods(string);
int dots = _countPeriods(p, string);
if (dots >= 0) { // negative if not simple number
try {
if (dots == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.datatype.jsr310.failing;
package com.fasterxml.jackson.datatype.jsr310.deser;

import java.time.Instant;
import java.util.Locale;
Expand All @@ -18,7 +18,7 @@

// [modules-java8#291] InstantDeserializer fails to parse negative numeric timestamp strings for
// pre-1970 values.
public class InstantDeserializerNegativeNumericTimestampString291Testest
public class InstantDeser291Test
extends ModuleTestBase
{
private final JsonMapper MAPPER = JsonMapper.builder()
Expand Down

0 comments on commit 45725b4

Please sign in to comment.