mongo-jackson-codec
mongo-jackson-codec copied to clipboard
Find using Java 8 time on collection fails with BsonSerializationExecption
I'm trying to do a find on a collection using a OffsetDateTime as one of the find parameters. For example
collection.find(and(eq("config_key", "foo"), lt("started_at", OffsetDateTime.now()))).first();
returns the exception below. I am registering the JavaTimeBsonModule
correctly before connecting to the database as I am able to serialize classes containing OffsetDateTimes just fine. This example also does not work with ZonedDateTime and Instant.
2016-06-20T20:16:22.009953849Z Exception in thread "main" org.bson.BsonSerializationException: Detected unknown BSON type "\x55" for fieldname "". Are you using the latest driver version?
2016-06-20T20:16:22.012424329Z at org.bson.BsonBinaryReader.readBsonType(BsonBinaryReader.java:94)
2016-06-20T20:16:22.013202845Z at org.bson.AbstractBsonWriter.pipeDocument(AbstractBsonWriter.java:698)
2016-06-20T20:16:22.014603469Z at org.bson.AbstractBsonWriter.pipe(AbstractBsonWriter.java:692)
2016-06-20T20:16:22.015978394Z at org.bson.codecs.RawBsonDocumentCodec.encode(RawBsonDocumentCodec.java:49)
2016-06-20T20:16:22.022486832Z at org.bson.codecs.RawBsonDocumentCodec.encode(RawBsonDocumentCodec.java:37)Listening for transport dt_socket at address: 33333
2016-06-20T20:16:22.022536944Z
2016-06-20T20:16:22.025803344Z at fr.javatic.mongo.jacksonCodec.JacksonCodec.encode(JacksonCodec.java:58)
2016-06-20T20:16:22.025845758Z at com.mongodb.client.model.BuildersHelper.encodeValue(BuildersHelper.java:35)
2016-06-20T20:16:22.025851109Z at com.mongodb.client.model.Filters$OperatorFilter.toBsonDocument(Filters.java:878)
2016-06-20T20:16:22.025856020Z at com.mongodb.FindIterableImpl.createQueryOperation(FindIterableImpl.java:172)
2016-06-20T20:16:22.025859971Z at com.mongodb.FindIterableImpl.execute(FindIterableImpl.java:167)
2016-06-20T20:16:22.025863329Z at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:148)
There is a workaround where I convert the OffsetDateTime to a Java 7 date before doing the find. collection.find(and(eq("config_key", "foo"), lt("started_at", Date.from(OffsetDateTime.now().toInstant())))).first();
Thanks for reporting, I'll take a look soon
Hi,
I've added an OffsetDateTime
to the codec test, and all seems ok at codec level.
I've published a 3.2.2_0.5
version with dependencies upgrade, can you try it ?
If it still fail, can you provide me a self-contained test case ?
Thanks.
I'm receiving a different error now
Exception in thread "main" org.bson.BsonSerializationException: Size -279948150 is not valid because it is negative.
at org.bson.BsonBinaryReader.readSize(BsonBinaryReader.java:360)
at org.bson.BsonBinaryReader.doReadStartDocument(BsonBinaryReader.java:257)
at org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:422)
at org.bson.AbstractBsonWriter.pipeDocument(AbstractBsonWriter.java:696)
at org.bson.AbstractBsonWriter.pipe(AbstractBsonWriter.java:692)
at org.bson.codecs.RawBsonDocumentCodec.encode(RawBsonDocumentCodec.java:49)
at org.bson.codecs.RawBsonDocumentCodec.encode(RawBsonDocumentCodec.java:37)
at fr.javatic.mongo.jacksonCodec.JacksonCodec.encode(JacksonCodec.java:58)
at com.mongodb.client.model.BuildersHelper.encodeValue(BuildersHelper.java:35)
at com.mongodb.client.model.Filters$SimpleEncodingFilter.toBsonDocument(Filters.java:1019)
at com.mongodb.FindIterableImpl.createQueryOperation(FindIterableImpl.java:172)
at com.mongodb.FindIterableImpl.execute(FindIterableImpl.java:167)
at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:148)
at com.golf1052.time.SaveTest.main(SaveTest.java:35)
I have created a repository here as an example. The problem line is L35 when trying to do a find using a OffsetDateTime.
The problem is the usage of RawBsonDocument
to encode all BSON values which is incorrect because not all BSON values are documents. The value is being serialized into an embedded document and then being parsed as a literal value (ObjectMapper.readValue
) which won't work for non-document values.