Skip to content

Commit 4e60947

Browse files
authored
Apply configured uuid representation to DB#command encoding and decoding (#861)
JAVA-4450
1 parent 464bcc8 commit 4e60947

File tree

6 files changed

+60
-19
lines changed

6 files changed

+60
-19
lines changed

driver-legacy/src/main/com/mongodb/CommandResult.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import com.mongodb.lang.Nullable;
2020
import org.bson.BsonDocument;
21+
import org.bson.BsonDocumentReader;
22+
import org.bson.codecs.Decoder;
23+
import org.bson.codecs.DecoderContext;
2124

22-
import static com.mongodb.DBObjects.toDBObject;
2325
import static com.mongodb.assertions.Assertions.notNull;
2426

2527
/**
@@ -39,14 +41,14 @@ public class CommandResult extends BasicDBObject {
3941
*/
4042
private final ServerAddress address;
4143

42-
CommandResult(final BsonDocument response) {
43-
this(response, null);
44+
CommandResult(final BsonDocument response, final Decoder<DBObject> decoder) {
45+
this(response, decoder, null);
4446
}
4547

46-
CommandResult(final BsonDocument response, @Nullable final ServerAddress address) {
48+
CommandResult(final BsonDocument response, final Decoder<DBObject> decoder, @Nullable final ServerAddress address) {
4749
this.address = address;
4850
this.response = notNull("response", response);
49-
putAll(toDBObject(response));
51+
putAll(decoder.decode(new BsonDocumentReader(response), DecoderContext.builder().build()));
5052
}
5153

5254
/**

driver-legacy/src/main/com/mongodb/DB.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import java.util.concurrent.ConcurrentHashMap;
4747

4848
import static com.mongodb.DBCollection.createWriteConcernException;
49-
import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry;
5049
import static com.mongodb.MongoNamespace.checkDatabaseNameValidity;
5150
import static com.mongodb.ReadPreference.primary;
5251
import static com.mongodb.assertions.Assertions.notNull;
@@ -84,7 +83,7 @@ public class DB {
8483
this.name = name;
8584
this.executor = executor;
8685
this.collectionCache = new ConcurrentHashMap<String, DBCollection>();
87-
this.commandCodec = new DBObjectCodec(getDefaultCodecRegistry());
86+
this.commandCodec = new DBObjectCodec(mongo.getCodecRegistry());
8887
}
8988

9089
/**
@@ -454,7 +453,7 @@ public CommandResult command(final DBObject command, final ReadPreference readPr
454453
try {
455454
return executeCommand(wrap(command, encoder), getCommandReadPreference(command, readPreference));
456455
} catch (MongoCommandException ex) {
457-
return new CommandResult(ex.getResponse(), ex.getServerAddress());
456+
return new CommandResult(ex.getResponse(), getDefaultDBObjectCodec(), ex.getServerAddress());
458457
}
459458
}
460459

@@ -522,7 +521,7 @@ public String toString() {
522521
CommandResult executeCommand(final BsonDocument commandDocument, final ReadPreference readPreference) {
523522
return new CommandResult(executor.execute(new CommandReadOperation<BsonDocument>(getName(), commandDocument,
524523
new BsonDocumentCodec()),
525-
readPreference, getReadConcern()));
524+
readPreference, getReadConcern()), getDefaultDBObjectCodec());
526525
}
527526

528527
OperationExecutor getExecutor() {
@@ -566,6 +565,13 @@ ReadPreference getCommandReadPreference(final DBObject command, @Nullable final
566565
}
567566
}
568567

568+
Codec<DBObject> getDefaultDBObjectCodec() {
569+
return new DBObjectCodec(getMongoClient().getCodecRegistry(),
570+
DBObjectCodec.getDefaultBsonTypeClassMap(),
571+
new DBCollectionObjectFactory())
572+
.withUuidRepresentation(getMongoClient().getMongoClientOptions().getUuidRepresentation());
573+
}
574+
569575
private static final Set<String> OBEDIENT_COMMANDS = new HashSet<String>();
570576

571577
static {

driver-legacy/src/main/com/mongodb/DBCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ public CommandResult explainAggregate(final List<? extends DBObject> pipeline, f
12651265
.collation(options.getCollation())
12661266
.retryReads(retryReads);
12671267
return new CommandResult(executor.execute(operation.asExplainableOperation(ExplainVerbosity.QUERY_PLANNER, new BsonDocumentCodec()),
1268-
primaryPreferred(), getReadConcern()));
1268+
primaryPreferred(), getReadConcern()), getDefaultDBObjectCodec());
12691269
}
12701270

12711271
@SuppressWarnings("unchecked")

driver-legacy/src/test/functional/com/mongodb/DBTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
import com.mongodb.internal.operation.ListCollectionsOperation;
2525
import org.bson.BsonDocument;
2626
import org.bson.BsonString;
27+
import org.bson.UuidRepresentation;
2728
import org.bson.codecs.BsonDocumentCodec;
2829
import org.junit.Test;
2930

3031
import java.util.Locale;
32+
import java.util.UUID;
3133

3234
import static com.mongodb.ClusterFixture.disableMaxTimeFailPoint;
3335
import static com.mongodb.ClusterFixture.enableMaxTimeFailPoint;
@@ -40,6 +42,7 @@
4042
import static com.mongodb.Fixture.getDefaultDatabaseName;
4143
import static com.mongodb.Fixture.getMongoClient;
4244
import static com.mongodb.ReadPreference.secondary;
45+
import static com.mongodb.client.Fixture.getMongoClientSettingsBuilder;
4346
import static org.hamcrest.CoreMatchers.containsString;
4447
import static org.hamcrest.CoreMatchers.hasItem;
4548
import static org.hamcrest.CoreMatchers.hasItems;
@@ -320,6 +323,26 @@ public void shouldRunCommandAgainstSecondaryWhenOnlySecondaryReadPreferenceSpeci
320323
assertThat((String) commandResult.get("serverUsed"), not(containsString(":27017")));
321324
}
322325

326+
@Test
327+
public void shouldApplyUuidRepresentationToCommandEncodingAndDecoding() {
328+
try (MongoClient client = new MongoClient(getMongoClientSettingsBuilder()
329+
.uuidRepresentation(UuidRepresentation.STANDARD)
330+
.build())) {
331+
// given
332+
UUID id = UUID.randomUUID();
333+
DB db = client.getDB(getDefaultDatabaseName());
334+
db.getCollection(collectionName).insert(new BasicDBObject("_id", id));
335+
336+
// when
337+
DBObject reply = db.command(new BasicDBObject("findAndModify", collectionName)
338+
.append("query", new BasicDBObject("_id", id))
339+
.append("remove", true));
340+
341+
// then
342+
assertThat((UUID) ((DBObject) reply.get("value")).get("_id"), is(id));
343+
}
344+
}
345+
323346
BsonDocument getCollectionInfo(final String collectionName) {
324347
return new ListCollectionsOperation<BsonDocument>(getDefaultDatabaseName(), new BsonDocumentCodec())
325348
.filter(new BsonDocument("name", new BsonString(collectionName))).execute(getBinding()).next().get(0);

driver-legacy/src/test/unit/com/mongodb/CommandResultTest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.bson.BsonDouble;
2222
import org.bson.BsonInt32;
2323
import org.bson.BsonString;
24+
import org.bson.codecs.Codec;
2425
import org.junit.Test;
2526

2627
import java.net.UnknownHostException;
@@ -33,53 +34,58 @@
3334

3435
public class CommandResultTest {
3536

37+
private static final Codec<DBObject> DECODER = MongoClientSettings.getDefaultCodecRegistry().get(DBObject.class);
38+
3639
@Test
3740
public void shouldBeOkWhenOkFieldIsTrue() throws UnknownHostException {
38-
CommandResult commandResult = new CommandResult(new BsonDocument("ok", BsonBoolean.TRUE));
41+
CommandResult commandResult =
42+
new CommandResult(
43+
new BsonDocument("ok", BsonBoolean.TRUE), DECODER);
3944
assertTrue(commandResult.ok());
4045
}
4146

4247
@Test
4348
public void shouldNotBeOkWithNoOkField() throws UnknownHostException {
44-
CommandResult commandResult = new CommandResult(new BsonDocument());
49+
CommandResult commandResult = new CommandResult(new BsonDocument(), DECODER);
4550
assertFalse(commandResult.ok());
4651
}
4752

4853
@Test
4954
public void shouldNotBeOkWhenOkFieldIsFalse() throws UnknownHostException {
50-
CommandResult commandResult = new CommandResult(new BsonDocument());
55+
CommandResult commandResult = new CommandResult(new BsonDocument(), DECODER);
5156
commandResult.put("ok", false);
5257
assertFalse(commandResult.ok());
5358
}
5459

5560
@Test
5661
public void shouldBeOkWhenOkFieldIsOne() throws UnknownHostException {
57-
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonDouble(1.0)));
62+
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonDouble(1.0)), DECODER);
5863
assertTrue(commandResult.ok());
5964
}
6065

6166
@Test
6267
public void shouldNotBeOkWhenOkFieldIsZero() throws UnknownHostException {
63-
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonDouble(0.0)));
68+
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonDouble(0.0)), DECODER);
6469
assertFalse(commandResult.ok());
6570
}
6671

6772
@Test
6873
public void shouldNotHaveExceptionWhenOkIsTrue() throws UnknownHostException {
69-
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonBoolean(true)));
74+
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonBoolean(true)), DECODER);
7075
assertNull(commandResult.getException());
7176
}
7277

7378
@Test
7479
public void shouldNotBeOkWhenOkFieldTypeIsNotBooleanOrNumber() throws UnknownHostException {
75-
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonString("1")));
80+
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonString("1")), DECODER);
7681
assertFalse(commandResult.ok());
7782
}
7883

7984
@Test
8085
public void testNullErrorCode() throws UnknownHostException {
8186
try {
82-
new CommandResult(new BsonDocument("ok", new BsonInt32(0)), new ServerAddress()).throwOnError();
87+
new CommandResult(new BsonDocument("ok", new BsonInt32(0)), DECODER, new ServerAddress())
88+
.throwOnError();
8389
fail("Should throw");
8490
} catch (MongoCommandException e) {
8591
assertEquals(-1, e.getCode());
@@ -91,7 +97,8 @@ public void testCommandFailure() throws UnknownHostException {
9197
try {
9298
new CommandResult(new BsonDocument("ok", new BsonInt32(0))
9399
.append("errmsg", new BsonString("ns not found"))
94-
.append("code", new BsonInt32(5000)), new ServerAddress()).throwOnError();
100+
.append("code", new BsonInt32(5000)), DECODER, new ServerAddress())
101+
.throwOnError();
95102
fail("Should throw");
96103
} catch (MongoCommandException e) {
97104
assertEquals(5000, e.getCode());

driver-legacy/src/test/unit/com/mongodb/DBSpecification.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class DBSpecification extends Specification {
7373
given:
7474
def mongo = Stub(MongoClient)
7575
mongo.mongoClientOptions >> MongoClientOptions.builder().build()
76+
mongo.codecRegistry >> getDefaultCodecRegistry()
7677
def executor = new TestOperationExecutor([1L, 2L, 3L])
7778
def db = new DB(mongo, 'test', executor)
7879
db.setReadConcern(ReadConcern.MAJORITY)
@@ -204,6 +205,7 @@ class DBSpecification extends Specification {
204205
given:
205206
def mongo = Stub(MongoClient)
206207
mongo.mongoClientOptions >> MongoClientOptions.builder().build()
208+
mongo.codecRegistry >> getDefaultCodecRegistry()
207209
def executor = new TestOperationExecutor([new BsonDocument('ok', new BsonDouble(1.0))])
208210
def database = new DB(mongo, 'test', executor)
209211
database.setReadPreference(ReadPreference.secondary())
@@ -235,6 +237,7 @@ class DBSpecification extends Specification {
235237
given:
236238
def mongo = Stub(MongoClient)
237239
mongo.mongoClientOptions >> MongoClientOptions.builder().build()
240+
mongo.codecRegistry >> getDefaultCodecRegistry()
238241
def executor = new TestOperationExecutor([new BsonDocument('ok', new BsonDouble(1.0))])
239242
def database = new DB(mongo, 'test', executor)
240243
database.setReadPreference(ReadPreference.secondary())

0 commit comments

Comments
 (0)