[JAVA-2419] org.bson.codecs.IterableCodec throws NPE when decoding ARRAYs Created: 23/Dec/16  Updated: 11/Jan/17  Resolved: 09/Jan/17

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

Type: Bug Priority: Major - P3
Reporter: Michael Weber Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Assuming you have a structure like the following within mongo db and try to decode it manually, not using org.bson.codecs.DocumentCodec a NullPointerException is being thrown if you trty to decode a nested ARRAY structure.

database-object:

{ "_id" : ObjectId("585d002202ef6c0c985cd350"), "listOfList" : [ [ ] ] }

Stacktrace:

java.lang.NullPointerException
	at org.bson.codecs.IterableCodec.readValue(IterableCodec.java:118)
	at org.bson.codecs.IterableCodec.decode(IterableCodec.java:78)
	at org.bson.codecs.IterableCodec.decode(IterableCodec.java:37)

SampleCode to replay:

        Codec<List> listCodec = mongoClient.getMongoClientOptions().getCodecRegistry().get(List.class);
        List decode = listCodec.decode(new JsonReader("[ [ ] ]"), DecoderContext.builder().build());

Remark:
If using the org.bson.codecs.DocumentCodec array structures are handled by org.bson.codecs.DocumentCodec itself, so there the problem does not exist.

I am not 100% sure tht IterableCodec should be able to handle Arrays, I just saw the comments in org.bson.codecs.BsonTypeClassMap stating that ARRAYS are supported. Unfortunately there is no mapping in th defaults section.



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

Author:

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

Message: JAVA-2419: Remove unnecessary Java 8 dependency from test
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/8fc0099d093ba6db577f579223ccf71936766187

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-2419: Add default mapping of BsonType.ARRAY to List.class in BsonTypeClassMap

Lazily throw exceptions for missing codecs in BsonTypeCodecMap. This allows use of BsonTypeCodecMap
for unmapped types so long as the codec that uses it doesn't actually request a codec for that type.
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/525e5ef12e15ede1c774e6597e3f1cbc1b6d02dd

Comment by Jeffrey Yemin [ 23/Dec/16 ]

It should be able to handle arrays. You can work around it like this:

        Map<BsonType, Class<?>> replacements = new HashMap<>();
        replacements.put(BsonType.ARRAY, Iterable.class);
        IterableCodecProvider iterableCodecProvider = new IterableCodecProvider(new BsonTypeClassMap(replacements));
        CodecRegistry registry = CodecRegistries.fromRegistries(CodecRegistries.fromProviders(iterableCodecProvider), MongoClient.getDefaultCodecRegistry());
        Codec<List> listCodec = registry.get(List.class);
        List decode = listCodec.decode(new JsonReader("[ [ 1, 2, 3 ] ]"), DecoderContext.builder().build());

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