Skip to content

Commit

Permalink
Fix CodecConfigurationException on uploading nested metadata to GridFS (
Browse files Browse the repository at this point in the history
#313)

See #291

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont committed Nov 8, 2023
1 parent 1546a0f commit 4d4f07f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/vertx/ext/mongo/impl/MongoClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ public Future<MongoGridFsClient> createDefaultGridFsBucketService() {

@Override
public MongoClient createGridFsBucketService(String bucketName, Handler<AsyncResult<MongoGridFsClient>> resultHandler) {
MongoGridFsClientImpl impl = new MongoGridFsClientImpl(vertx, this, getGridFSBucket(bucketName));
MongoGridFsClientImpl impl = new MongoGridFsClientImpl(vertx, this, getGridFSBucket(bucketName), holder.db.getCodecRegistry());
resultHandler.handle(Future.succeededFuture(impl));
return this;
}
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/io/vertx/ext/mongo/impl/MongoGridFsClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
import io.vertx.core.json.JsonObject;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
import io.vertx.ext.mongo.*;
import io.vertx.ext.mongo.GridFsDownloadOptions;
import io.vertx.ext.mongo.GridFsUploadOptions;
import io.vertx.ext.mongo.MongoGridFsClient;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

Expand All @@ -38,11 +44,13 @@ public class MongoGridFsClientImpl implements MongoGridFsClient {
private final GridFSBucket bucket;
private final MongoClientImpl clientImpl;
private final VertxInternal vertx;
private final CodecRegistry codecRegistry;

public MongoGridFsClientImpl(VertxInternal vertx, MongoClientImpl mongoClient, GridFSBucket gridFSBucket) {
public MongoGridFsClientImpl(VertxInternal vertx, MongoClientImpl mongoClient, GridFSBucket gridFSBucket, CodecRegistry codecRegistry) {
this.vertx = vertx;
this.clientImpl = mongoClient;
this.bucket = gridFSBucket;
this.codecRegistry = codecRegistry;
}

@Override
Expand Down Expand Up @@ -71,14 +79,22 @@ public MongoGridFsClient uploadByFileNameWithOptions(ReadStream<Buffer> stream,
public Future<String> uploadByFileNameWithOptions(ReadStream<Buffer> stream, String fileName, GridFsUploadOptions options) {
GridFSUploadOptions uploadOptions = new GridFSUploadOptions();
uploadOptions.chunkSizeBytes(options.getChunkSizeBytes());
if (options.getMetadata() != null) uploadOptions.metadata(new Document(options.getMetadata().getMap()));
if (options.getMetadata() != null) {
uploadOptions.metadata(wrap(options.getMetadata()));
}

GridFSReadStreamPublisher publisher = new GridFSReadStreamPublisher(stream);
Promise<ObjectId> promise = vertx.promise();
bucket.uploadFromPublisher(fileName, publisher, uploadOptions).subscribe(new SingleResultSubscriber<>(promise));
return promise.future().map(ObjectId::toHexString);
}

private Document wrap(JsonObject json) {
Codec<Document> codec = codecRegistry.get(Document.class);
BsonDocument bsonDocument = new JsonObjectBsonAdapter(json).toBsonDocument(BsonDocument.class, codecRegistry);
return codec.decode(bsonDocument.asBsonReader(), DecoderContext.builder().build());
}

@Override
public MongoGridFsClient uploadFile(String fileName, Handler<AsyncResult<String>> resultHandler) {
Future<String> future = uploadFile(fileName);
Expand Down Expand Up @@ -115,7 +131,7 @@ public Future<String> uploadFileWithOptions(String fileName, GridFsUploadOptions
GridFSUploadOptions uploadOptions = new GridFSUploadOptions();
uploadOptions.chunkSizeBytes(options.getChunkSizeBytes());
if (options.getMetadata() != null) {
uploadOptions.metadata(new Document(options.getMetadata().getMap()));
uploadOptions.metadata(wrap(options.getMetadata()));
}
bucket.uploadFromPublisher(fileName, publisher, uploadOptions).subscribe(new SingleResultSubscriber<>(promise));
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/vertx/ext/mongo/GridFsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.vertx.core.file.OpenOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.core.streams.ReadStream;

import org.junit.Test;

import java.io.File;
Expand Down Expand Up @@ -234,7 +233,7 @@ public void testFindWithMetadata() {
gridFsClient.get().findIds(query, findPromise);
return findPromise.future();
}).compose(list -> {
assertTrue(list.size() > 0);
assertFalse(list.isEmpty());
testComplete();
return Future.succeededFuture();
}).onComplete(event -> {
Expand Down Expand Up @@ -671,7 +670,8 @@ public void testStreamUploadWithOptions() {
String fileName = createTempFileWithContent(1024);
GridFsUploadOptions options = new GridFsUploadOptions();
options.setChunkSizeBytes(1024);
options.setMetadata(new JsonObject().put("meta_test", "Kamapua`a"));
options.setMetadata(new JsonObject().put("meta_test", "Kamapua`a")
.put("nested", new JsonObject().put("foobar", true)));

AtomicReference<MongoGridFsClient> gridFsClient = new AtomicReference<>();

Expand Down

0 comments on commit 4d4f07f

Please sign in to comment.