[JAVA-2022] JsonReader does not accept ISODate() Created: 28/Oct/15  Updated: 07/Dec/15  Resolved: 30/Oct/15

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: 3.0.4
Fix Version/s: 3.2.0

Type: Improvement Priority: Minor - P4
Reporter: Patrick Suter Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to JAVA-2024 JsonReader handles `Date()` incorrectly Closed

 Description   

Hi

The org.bson.json.JsonReader does not accept fields with ISODate() as content for a current date. Since this is possible in the shell and the JsonReader already accepts Date() as input it would be nice if ISODate() would share the same behaviour. Or am i missing something?

In visitDateTimeConstructor() this case is already handled like this:

JsonToken token = popToken();
if (token.getType() == JsonTokenType.RIGHT_PAREN) {
            return new Date().getTime();
        } 

Thanks for looking into this!



 Comments   
Comment by Jeffrey Yemin [ 07/Dec/15 ]

The 3.2.0 driver, which contains a fix for this issue, has been released today.

Comment by Githook User [ 29/Oct/15 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Add Json support for new ISODate()

JAVA-2022
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/5370977b0a88318284935d442de1218bd46a4547

Comment by Patrick Suter [ 28/Oct/15 ]

Of course here is the simplest example i could make:

CodecRegistry registry = fromProviders(asList(new ValueCodecProvider(), new BsonValueCodecProvider(), new DocumentCodecProvider()));
String workingJson = "{now : Date()}";
DBObject now = new DBObjectCodec(registry).decode(new JsonReader(workingJson), DecoderContext.builder().build());
String invalidJson = "{now : ISODate()}";
DBObject object = new DBObjectCodec(registry).decode(new JsonReader(invalidJson), DecoderContext.builder().build());

The first decode() works as intended and creates a BsonDateTime Object with the current timestamp.
The second result in an Exception:

Caused by: org.bson.json.JsonParseException: JSON reader expected a string but found ')'.
	at org.bson.json.JsonReader.visitISODateTimeConstructor(JsonReader.java:733) ~[mongo-java-driver-3.0.4.jar:na]
	at org.bson.json.JsonReader.readBsonType(JsonReader.java:191) ~[mongo-java-driver-3.0.4.jar:na]
	at com.mongodb.DBObjectCodec.readDocument(DBObjectCodec.java:343) ~[mongo-java-driver-3.0.4.jar:na]
	at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:136) ~[mongo-java-driver-3.0.4.jar:na]

or in the style of your tests:

String validDate = "Date()";
new JsonReader(validDate); // works
String invalidDate = "ISODate()";
new JsonReader(invalidDate); // throws exception

Comment by Jeffrey Yemin [ 28/Oct/15 ]

Hi Patrick,

If possible, please provide a code snippet that reproduces the issue.

We have a couple of tests for ISODate, and I'm wondering what's different in your scenario.

Generated at Thu Feb 08 08:56:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.