[JAVA-2404] Cannot use BasicDBList as value with Updates builder methods Created: 09/Dec/16  Updated: 09/Jan/17  Resolved: 09/Jan/17

Status: Closed
Project: Java Driver
Component/s: Codecs
Affects Version/s: 3.0.0
Fix Version/s: 3.5.0

Type: Bug Priority: Minor - P4
Reporter: Giordano Maestro Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Zip Archive test-case-bug.zip    

 Description   

If you use the Updates class in this way Updates.set("array", value) where value is a BasicDbList Object the result of the insert is

{
"_id" : "ID",
"array" :

{ "0" : 100, "1" : 200, "2" : 400 }

}

The list BasicDBList is now a document instead of a list



 Comments   
Comment by Githook User [ 09/Jan/17 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-2404: Prevent DBObjectCodecProvider from providing a Codec for a DBObject that is also a List.

Othewise, it will provide the DBObjectCodec for BasicDBList which is incorrect. With this change,
the IterableCodecProvider will end up providing ListCodec, which is the correct thing to do.
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/f10bebf2db5b2c9feb8adc24718a4e836c37950e

Comment by Ross Lawley [ 09/Dec/16 ]

Hi Giordano Maestro,

Thanks for the ticket, this is an interesting corner case and essentially is a configuration issue with the codec registry.

The BasicDbList is a subclass of DBObject due to the class reflecting the way BSON lists work. When the Updates.set helper looks up the class for the value in the CodecRegistry its returned a DBObjectCodec as its seeing a DBObject class and treating it as such.

There are a couple of work arounds, one would be to add a specific codec to handle BasicDBLists, or a simpler way would be to explicitly convert the BasicDbList to a List eg:

Updates.set("array",  Arrays.asList(value.toArray()))

Ross

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