[JAVA-1106] Empty objects and arrays not handled correctly in results. Created: 05/Feb/14  Updated: 16/Mar/15  Resolved: 16/Mar/15

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: 2.11.4
Fix Version/s: 3.0.0

Type: Bug Priority: Minor - P4
Reporter: Graham Thomson Assignee: Jeffrey Yemin
Resolution: Done Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Any


Backwards Compatibility: Major Change

 Description   

Values in an object returned from a query may be an empty object or an empty array.

If such cases, calling bo.keySet().isEmpty() always returns false (the result is hardwired in the method override), depsite the fact that bo.keySet().size() == 0.

A simple fix is to change the org.bson.util.StringRangeSet.isEmpty() implementation to:

@Override
public boolean isEmpty()

{ return size == 0; }

 Comments   
Comment by Jeffrey Yemin [ 16/Mar/15 ]

Tested in master, which has a simplified implementation of StringRangeSet, and isEmpty works properly.

Comment by Graham Thomson [ 06/May/14 ]

I can refine it a little - as pointed out, the problem pertains to org.bson.util.StringRangeSet. After digging deeper into the code I realise that this is only used by org.bson.types.BasicBSONList. So my apologies - it is actually only arrays that are not handled correctly in results.

The following snippet reproduces the problem:

 
    public static void main(String[] args) throws UnknownHostException {
        MongoClient mongoClient = new MongoClient();
        mongoClient.getDB("empty_test_db").dropDatabase();
 
        DBCollection collection = mongoClient.getDB("empty_test_db").getCollection("empty_test_coll");
        collection.insert(new BasicDBObject("emptyArray", Collections.EMPTY_LIST));
 
        DBObject empty = collection.find().next();
        DBObject emptyArray = (DBObject) empty.get("emptyArray");
        System.out.println("emptyArray keySet empty? " + emptyArray.keySet().isEmpty() + ", size: " + emptyArray.keySet().size() + ", " + emptyArray.keySet().getClass());
    }
 

And produces the following output:

emptyArray keySet empty? false, size: 0, class org.bson.util.StringRangeSet

Comment by Michael Reynolds [ 04/May/14 ]

Can you provide steps to reproduce this issue?

Comment by Jeffrey Yemin [ 06/Feb/14 ]

Never mind, I thought you meant that this happens with empty documents also.

Comment by Jeffrey Yemin [ 05/Feb/14 ]

Would you mind adding a small test program that demonstrates the problem?

Thanks,
Jeff

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