[JAVA-2430] Do we need to assert not null for filename? Created: 22/Jan/17  Updated: 11/Sep/19  Resolved: 04/Jul/18

Status: Closed
Project: Java Driver
Component/s: GridFS
Affects Version/s: 3.4.0
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Daniel Weller Assignee: Unassigned
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends

 Description   

When using the driver and gridfs I get an error `java.lang.IllegalArgumentException: filename can not be null`. The data does not have a filename attribute in the gridfs store.

The source code shows that we are asserting not null for the filename just like the error tells us:
https://github.com/mongodb/mongo-java-driver/blob/master/driver-core/src/main/com/mongodb/client/gridfs/model/GridFSFile.java#L81

So do we have to assert not null for filename? My files will never have a filename that matters/is worth my time to create a filename on the way into gridfs.

java.lang.IllegalArgumentException: filename can not be null
	at com.mongodb.assertions.Assertions.notNull(Assertions.java:37) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.client.gridfs.model.GridFSFile.<init>(GridFSFile.java:81) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.client.gridfs.model.GridFSFile.<init>(GridFSFile.java:61) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.client.gridfs.GridFSFindIterableImpl$1.apply(GridFSFindIterableImpl.java:143) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.client.gridfs.GridFSFindIterableImpl$1.apply(GridFSFindIterableImpl.java:122) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.MongoMappingCursor.next(MongoMappingCursor.java:44) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.MappingIterable.first(MappingIterable.java:45) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.client.gridfs.GridFSFindIterableImpl.first(GridFSFindIterableImpl.java:102) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.client.gridfs.GridFSFindIterableImpl.first(GridFSFindIterableImpl.java:45) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.client.gridfs.GridFSBucketImpl.findTheFileInfoAndOpenDownloadStream(GridFSBucketImpl.java:292) ~[mongo-java-driver-3.2.2.jar:na]
	at com.mongodb.client.gridfs.GridFSBucketImpl.openDownloadStream(GridFSBucketImpl.java:161)

Code:

MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
MongoDatabase mongoDatabase = mongoClient.getDatabase("db");
GridFSBucket gridFSBucket = GridFSBuckets.create(mongoDatabase);
ObjectId fileId = new ObjectId(Id);
GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);



 Comments   
Comment by Ross Lawley [ 04/Jul/18 ]

Fixed in JAVA-2546

Comment by Daniel Weller [ 05/Feb/17 ]

Thank you. Using an empty string isn't straight forward for me so instead I modified the source code and built my own jar using 3.5.0-SNAPSHOT. Also I looked for alternative projects that work with gridfs methods and the ones that I found seemed to just be using mongo-java-driver methods underneath. The mongo-java-driver should follow the mongo specification and I hope it isn't too hard to make the changes to the project.

Comment by Jeffrey Yemin [ 27/Jan/17 ]

danweller18 we're discussing this internally and will get back to you on this ticket. In the meantime, as a workaround you should use an empty string if you don't require a filename.

Comment by Daniel Weller [ 27/Jan/17 ]

Why is filename required in mongodb java driver? According to https://docs.mongodb.com/manual/core/gridfs/#files.filename it is optional.

Comment by Jeffrey Yemin [ 26/Jan/17 ]

While filename is required, it need not be unique. So while null is not permitted, the empty string is. Will that suffice?

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