Skip to content

Commit fe21fa4

Browse files
authored
Fix #561: Deserializing empty timestamp fields to null value doesn't work (#825)
1 parent eb7bff9 commit fe21fa4

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

release-notes/VERSION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ Version: 3.x (for earlier see VERSION-2.x)
5151
polymorphic nested objects
5252
(reported by Vitor P)
5353
(fix by @cowtowncoder, w/ Claude code)
54+
#561: Deserializing empty timestamp fields to `null` value doesn't
55+
work (instead becomes "empty", Epoch time)
56+
(reported by @silvestr85)
57+
(fix by @cowtowncoder, w/ Claude code)
5458
#565: Blank text is ignored in `@JacksonXmlText`
5559
(reported by Alessandro A)
5660
(fix by @cowtowncoder, w/ Claude code)

src/main/java/tools/jackson/dataformat/xml/XmlMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public Builder(XmlFactory f) {
107107
.setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);
108108
_coercionConfigs.findOrCreateCoercion(LogicalType.Boolean)
109109
.setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);
110+
// 18-Mar-2026, tatu: [dataformat-xml#561] Same for Date/Time types
111+
_coercionConfigs.findOrCreateCoercion(LogicalType.DateTime)
112+
.setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);
110113
}
111114

112115
@Override
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package tools.jackson.dataformat.xml.deser;
2+
3+
import java.util.Date;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import com.fasterxml.jackson.annotation.JsonFormat;
8+
9+
import tools.jackson.dataformat.xml.XmlMapper;
10+
import tools.jackson.dataformat.xml.XmlTestUtil;
11+
import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
// [dataformat-xml#561] Empty timestamp/date attribute should deserialize as null
16+
public class EmptyTimestampDeser561Test extends XmlTestUtil
17+
{
18+
static class DateAttrBean {
19+
@JacksonXmlProperty(isAttribute = true)
20+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
21+
public Date timestampDate;
22+
}
23+
24+
static class DateElementBean {
25+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
26+
public Date timestampDate;
27+
}
28+
29+
static class DateBean {
30+
@JacksonXmlProperty(isAttribute = true)
31+
@JsonFormat(pattern = "yyyy-MM-dd")
32+
public Date dateValue;
33+
}
34+
35+
private final XmlMapper MAPPER = newMapper();
36+
37+
// [dataformat-xml#561]: empty attribute
38+
@Test
39+
public void testEmptyDateAttributeWithTimestampFormat() throws Exception
40+
{
41+
DateAttrBean result = MAPPER.readValue(
42+
"<DateAttrBean timestampDate=\"\" />",
43+
DateAttrBean.class);
44+
assertNotNull(result);
45+
assertNull(result.timestampDate);
46+
}
47+
48+
// [dataformat-xml#561]: empty element
49+
@Test
50+
public void testEmptyDateElement() throws Exception
51+
{
52+
DateElementBean result = MAPPER.readValue(
53+
"<DateElementBean><timestampDate></timestampDate></DateElementBean>",
54+
DateElementBean.class);
55+
assertNotNull(result);
56+
assertNull(result.timestampDate);
57+
}
58+
59+
// [dataformat-xml#561]: empty Date attribute
60+
@Test
61+
public void testEmptyDateAttribute() throws Exception
62+
{
63+
DateBean result = MAPPER.readValue(
64+
"<DateBean dateValue=\"\" />",
65+
DateBean.class);
66+
assertNotNull(result);
67+
assertNull(result.dateValue);
68+
}
69+
}

0 commit comments

Comments
 (0)