Skip to content

Commit ae9bfe9

Browse files
committed
Fix #249
1 parent f6b98df commit ae9bfe9

File tree

3 files changed

+49
-34
lines changed

3 files changed

+49
-34
lines changed

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class YearMonthDeserializer extends JSR310DateTimeDeserializerBase<YearMo
4545
*/
4646
public YearMonthDeserializer() // public since 2.12
4747
{
48-
this(DateTimeFormatter.ofPattern("uuuu-MM"));
48+
this(DateTimeFormatter.ofPattern("u-MM"));
4949
}
5050

5151
public YearMonthDeserializer(DateTimeFormatter formatter)

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserTest.java

+46-33
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
package com.fasterxml.jackson.datatype.jsr310.deser;
22

3+
import java.io.IOException;
4+
import java.time.Month;
5+
import java.time.YearMonth;
6+
import java.time.format.DateTimeParseException;
7+
import java.util.Map;
8+
39
import com.fasterxml.jackson.annotation.JsonFormat;
410
import com.fasterxml.jackson.core.JsonProcessingException;
511
import com.fasterxml.jackson.core.type.TypeReference;
612
import com.fasterxml.jackson.databind.DeserializationFeature;
713
import com.fasterxml.jackson.databind.JsonMappingException;
814
import com.fasterxml.jackson.databind.ObjectMapper;
915
import com.fasterxml.jackson.databind.ObjectReader;
16+
import com.fasterxml.jackson.databind.SerializationFeature;
1017
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
1118
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
1219

1320
import org.junit.Test;
1421

15-
import java.io.IOException;
16-
import java.time.Month;
17-
import java.time.YearMonth;
18-
import java.time.format.DateTimeParseException;
19-
import java.util.Map;
20-
2122
import static org.junit.Assert.assertEquals;
2223
import static org.junit.Assert.assertNotNull;
2324
import static org.junit.Assert.assertNull;
2425
import static org.junit.Assert.fail;
2526

2627
public class YearMonthDeserTest extends ModuleTestBase
2728
{
28-
private final ObjectReader READER = newMapper().readerFor(YearMonth.class);
29+
private final ObjectMapper MAPPER = newMapper();
30+
private final ObjectReader READER = MAPPER.readerFor(YearMonth.class);
2931
private final TypeReference<Map<String, YearMonth>> MAP_TYPE_REF = new TypeReference<Map<String, YearMonth>>() { };
3032

3133
@Test
@@ -35,52 +37,63 @@ public void testDeserializationAsString01() throws Exception
3537
}
3638

3739
@Test
38-
public void testBadDeserializationAsString01() throws Throwable
40+
public void testBadDeserializationAsString01() throws Exception
3941
{
4042
expectFailure("'notayearmonth'");
4143
}
4244

4345
@Test
44-
public void testDeserializationAsArrayDisabled() throws Throwable
46+
public void testDeserializationAsArrayDisabled() throws Exception
4547
{
46-
try {
47-
read("['2000-01']");
48-
fail("expected JsonMappingException");
48+
try {
49+
read("['2000-01']");
50+
fail("expected JsonMappingException");
4951
} catch (JsonMappingException e) {
5052
// OK
5153
} catch (IOException e) {
5254
throw e;
5355
}
54-
5556
}
5657

5758
@Test
58-
public void testDeserializationAsEmptyArrayDisabled() throws Throwable
59+
public void testDeserializationAsEmptyArrayDisabled() throws Exception
5960
{
60-
// works even without the feature enabled
61-
assertNull(read("[]"));
61+
// works even without the feature enabled
62+
assertNull(read("[]"));
6263
}
6364

6465
@Test
65-
public void testDeserializationAsArrayEnabled() throws Throwable
66+
public void testDeserializationAsArrayEnabled() throws Exception
6667
{
67-
String json="['2000-01']";
68-
YearMonth value= newMapper()
69-
.configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true)
70-
.readerFor(YearMonth.class).readValue(a2q(json));
71-
notNull(value);
68+
String json="['2000-01']";
69+
YearMonth value= newMapper()
70+
.configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true)
71+
.readerFor(YearMonth.class).readValue(a2q(json));
72+
notNull(value);
7273
expect(YearMonth.of(2000, Month.JANUARY), value);
7374
}
7475

7576
@Test
76-
public void testDeserializationAsEmptyArrayEnabled() throws Throwable
77+
public void testDeserializationAsEmptyArrayEnabled() throws Exception
78+
{
79+
String json="[]";
80+
YearMonth value = newMapper()
81+
.configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true)
82+
.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true)
83+
.readerFor(YearMonth.class).readValue(a2q(json));
84+
assertNull(value);
85+
}
86+
87+
// [modules-java8#249
88+
@Test
89+
public void testYearAbove10k() throws Exception
7790
{
78-
String json="[]";
79-
YearMonth value= newMapper()
80-
.configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true)
81-
.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true)
82-
.readerFor(YearMonth.class).readValue(a2q(json));
83-
assertNull(value);
91+
YearMonth input = YearMonth.of(10000, 1);
92+
String json = MAPPER.writer()
93+
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
94+
.writeValueAsString(input);
95+
YearMonth result = READER.readValue(json);
96+
expect(input, result);
8497
}
8598

8699
/*
@@ -126,7 +139,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception {
126139
objectReader.readValue(valueFromEmptyStr);
127140
}
128141

129-
private void expectFailure(String json) throws Throwable {
142+
private void expectFailure(String json) throws Exception {
130143
try {
131144
read(json);
132145
fail("expected DateTimeParseException");
@@ -135,20 +148,20 @@ private void expectFailure(String json) throws Throwable {
135148
throw e;
136149
}
137150
if (!(e.getCause() instanceof DateTimeParseException)) {
138-
throw e.getCause();
151+
throw (Exception) e.getCause();
139152
}
140153
} catch (IOException e) {
141154
throw e;
142155
}
143156
}
144157

145-
private void expectSuccess(Object exp, String json) throws IOException {
158+
private void expectSuccess(Object exp, String json) throws Exception {
146159
final YearMonth value = read(json);
147160
notNull(value);
148161
expect(exp, value);
149162
}
150163

151-
private YearMonth read(final String json) throws IOException {
164+
private YearMonth read(final String json) throws Exception {
152165
return READER.readValue(a2q(json));
153166
}
154167

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Modules:
1919
(contributed by (Maciej D)
2020
#242: Fix InstantSerializer ignoring the JsonFormat shape
2121
(contributed by KaseiFR@github)
22+
#249: `YearMonthDeserializer` fails for year > 9999
23+
(reported by bent-lorentzen@github)
2224
#251: Allow `Optional` deserialization for "absent" value as Java `null`
2325
(like other Reference types), not "empty"
2426

0 commit comments

Comments
 (0)