Skip to content

Commit d8b4ea9

Browse files
authored
Merge pull request #181 from Thadoy/old_date_fix
Bugfix: Deserialization of old dates in Mongo
2 parents 6ce0b80 + 8e66e90 commit d8b4ea9

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* Bugfix: Fixes problem with events time to live set to 10 seconds
2222
* Bugfix: Fixing problem with retry messages in Interconnect
2323
* Bugfix: Better error handling in CloudconductorPropertyProvider
24+
* Bugfix: Fallback for old dates (before 1970) for mongo
2425
* Fixed vulnerabilities: CVE-2024-13009(Jetty), CVE-2025-23184(Apache CXF), CVE-2024-57699 (Json-smart),CVE-2025-27533 (ActiveMQ)
2526
* Logging improvement and extension options for DaemonMessageListener
2627
* Add TLS server parameters for JAX-RS

mongodb/src/main/java/de/taimos/dvalin/mongo/mapper/JodaMapping.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@
4242
*/
4343
public class JodaMapping {
4444

45+
private static final String DATE_FIELD_NAME = "$date";
46+
4547
public static class MongoDateTimeSerializer extends JsonSerializer<DateTime> {
4648

4749
@Override
4850
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
4951
jgen.writeStartObject();
50-
jgen.writeStringField("$date", value.withZone(DateTimeZone.UTC).toString(ISODateTimeFormat.dateTime()));
52+
jgen.writeStringField(JodaMapping.DATE_FIELD_NAME, value.withZone(DateTimeZone.UTC).toString(ISODateTimeFormat.dateTime()));
5153
jgen.writeEndObject();
5254
}
5355

@@ -58,8 +60,22 @@ public static class MongoDateTimeDeserializer extends JsonDeserializer<DateTime>
5860
@Override
5961
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
6062
JsonNode node = jp.getCodec().readTree(jp);
61-
String date = node.get("$date").asText();
62-
return ISODateTimeFormat.dateTimeParser().withZoneUTC().parseDateTime(date);
63+
String date = node.get(JodaMapping.DATE_FIELD_NAME).asText();
64+
try {
65+
return ISODateTimeFormat.dateTimeParser().withZoneUTC().parseDateTime(date);
66+
} catch (IllegalArgumentException e) {
67+
// Fallback for old dates (before 1970)
68+
JsonNode dateNode = node.get(JodaMapping.DATE_FIELD_NAME);
69+
if (dateNode != null) {
70+
JsonNode numberLongNode = dateNode.get("$numberLong");
71+
if (numberLongNode != null) {
72+
// Parse as BSON timestamp (milliseconds since epoch)
73+
long timestamp = numberLongNode.asLong();
74+
return new DateTime(timestamp, DateTimeZone.UTC);
75+
}
76+
}
77+
}
78+
return null;
6379
}
6480
}
6581
}

0 commit comments

Comments
 (0)