[JAVA-1644] NPE on createIndex when the database name contains a "." Created: 08/Feb/15  Updated: 29/Nov/16  Resolved: 03/Nov/16

Status: Closed
Project: Java Driver
Component/s: Command Operations
Affects Version/s: 2.13.0
Fix Version/s: 3.4.0

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

Issue Links:
Duplicate
is duplicated by JAVA-2396 NullPointerException when dropping a ... Closed
Related
related to SERVER-26431 Validate the collection name for comm... Closed
is related to DRIVERS-2092 Drivers Spec : Specify object, colle... Closed

 Description   

I'm getting a NPE from the Mongo Java driver when calling createIndex on a DBCollection in a database which has a full stop (aka period, ".") in its name. Here's the exception ...

java.lang.NullPointerException
	at com.mongodb.DBCollectionImpl.createIndex(DBCollectionImpl.java:399)
	at com.mongodb.DBCollection.createIndex(DBCollection.java:597)
	at com.mongodb.DBCollection.createIndex(DBCollection.java:585)

Here's a reproduction test case:

    @Test(expected = NullPointerException.class)
    public void reproduceIssueWithNamesContainingAFullStop() {
        String databaseName = "dbname-v1.1";
        String collectionName = "whatever";
 
        DB db = mongo.getDB(databaseName);
        DBCollection dbCollection = db.getCollection(collectionName);
 
        DBObject indexKeys = new BasicDBObject("aField", 1);
        DBObject indexOptions = new BasicDBObject("unique", true);
        
        dbCollection.createIndex(indexKeys);
        Assert.fail("Expected Mongo Java driver to throw a NPE here ...");
    }



 Comments   
Comment by Githook User [ 03/Nov/16 ]

Author:

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

Message: JAVA-1644: Check validity of database name and collection name

Add static helper methods to MongoNamespace for checking validity prior to construction

Check validity of database name and collection name in high-level API
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/2a1858c0bbd825b774fc8f3f15c71c1d951f8a63

Comment by Jeffrey Yemin [ 17/Oct/16 ]

To prevent an obscure error message from being reported, the driver should validate that database names don't contain a '.'.

Comment by Jeffrey Yemin [ 17/Oct/16 ]

It appears that the reason the reply contains no documents is that the server treats it is a query on the 1.$cmd collection, which does not exist, rather than a command. The suggested fix in SERVER-26431 is to validate that collection name (which is not legal) and report an error rather than treating it a query.

Comment by Colm McDonnell [ 09/Feb/15 ]

Hi Ross,

I have added my own validation before calling the Mongo Java driver, so it's not affecting me now. I raised the JIRA to (a) get confirmation that this is a reserved character and (b) see if the driver could be changed to handle this as a known failure rather than a NPE.

So, your response hit the nail on the head, thanks!

Comment by Ross Lawley [ 09/Feb/15 ]

Hi [colmmcd,

The database name is not valid as /\ " . are invalid characters on all systems and there are a few more invalid names for Windows systems - see: naming restrictions. This explains why theres an error and I'll investigate why a null pointer error is being thrown rather than the invalid namespace error that I'd expect.

Comment by Colm McDonnell [ 08/Feb/15 ]

Here's the control test case, to show that the same code runs successfully when the databaseName does not contain a full stop ...

@Test
    public void noIssueWithNameWhichDoesNotContainAFullStop() {
        String databaseName = "dbname-v11";
        String collectionName = "whatever";
 
        DB db = mongo.getDB(databaseName);
        DBCollection dbCollection = db.getCollection(collectionName);
 
        DBObject indexKeys = new BasicDBObject("aField", 1);
        DBObject indexOptions = new BasicDBObject("unique", true);
 
        dbCollection.createIndex(indexKeys, indexOptions);
    }

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