[JAVA-2577] GridFSFileCodec throws exception when metadata field is null Created: 06/Aug/17  Updated: 27/Oct/23  Resolved: 06/Sep/17

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

Type: Bug Priority: Major - P3
Reporter: Mihály Gyöngyösi Assignee: Ross Lawley
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

macOS Sierra (10.12.6)



 Description   

GridFSFileCodec throws an exception when the file's "metadata" field is null. GridFSFileCodec.java#73

Underlying problem in the BsonDocument class (containsKey("metadata") return true when the value is BsonNull.

public BsonDocument getDocument(final Object key, final BsonDocument defaultValue) {
        if (!containsKey(key)) {
            return defaultValue;
        }
        return get(key).asDocument();
    }

Full stacktrace:

Exception in thread "main" org.bson.BsonInvalidOperationException: Value expected to be of type DOCUMENT is of unexpected type NULL
	at org.bson.BsonValue.throwIfInvalidType(BsonValue.java:419)
	at org.bson.BsonValue.asDocument(BsonValue.java:47)
	at org.bson.BsonDocument.getDocument(BsonDocument.java:506)
	at com.mongodb.client.gridfs.codecs.GridFSFileCodec.decode(GridFSFileCodec.java:73)
	at com.mongodb.client.gridfs.codecs.GridFSFileCodec.decode(GridFSFileCodec.java:48)
	at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
	at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60)
	at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
	at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
	at org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47)
	at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
	at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63)
	at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
	at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
	at com.mongodb.connection.ReplyMessage.<init>(ReplyMessage.java:50)
	at com.mongodb.connection.CommandProtocol.getResponseDocument(CommandProtocol.java:132)
	at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:111)
	at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
	at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
	at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:176)
	at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:216)
	at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:207)
	at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:113)
	at com.mongodb.operation.FindOperation$1.call(FindOperation.java:715)
	at com.mongodb.operation.FindOperation$1.call(FindOperation.java:709)
	at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:433)
	at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406)
	at com.mongodb.operation.FindOperation.execute(FindOperation.java:709)
	at com.mongodb.operation.FindOperation.execute(FindOperation.java:81)
	at com.mongodb.Mongo.execute(Mongo.java:810)
	at com.mongodb.Mongo$2.execute(Mongo.java:797)
	at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
	at com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:200)
	at com.mongodb.client.gridfs.GridFSFindIterableImpl.iterator(GridFSFindIterableImpl.java:88)



 Comments   
Comment by Ross Lawley [ 07/Aug/17 ]

Hi,

Correct, you should not include the field at all.

Ross

Comment by Mihály Gyöngyösi [ 07/Aug/17 ]

Hi ross.lawley,

Thank you for your answer. So it means when I don't want to add metadata to the GridFS file then I have to skip it and I should not add the field with null value?

This GridFS file/document generated by an other project and with "metadata" : null (Type: Null)

Mihály

Comment by Ross Lawley [ 07/Aug/17 ]

Hi mgyongyosi,

Thanks for the ticket, as you mention the GridFSCodec is expecting a document BSON type for the metadata field and not a NULL type. This follows the specification and expected format for the GridFS convention.

I'd be interested to learn, how you have come to have an alternative BSON type (null) in the metadata field?

Ross

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