Skip to content

Commit b2f9144

Browse files
authored
Include versioned API options in all commands (#715)
Previously they were excluded from getMore and transaction-continuing commands. Now they are included. JAVA-4128
1 parent 4f28ee0 commit b2f9144

File tree

6 files changed

+26
-208
lines changed

6 files changed

+26
-208
lines changed

driver-core/src/main/com/mongodb/internal/connection/CommandMessage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,14 @@ private List<BsonElement> getExtraElements(final SessionContext sessionContext)
284284
if (firstMessageInTransaction) {
285285
extraElements.add(new BsonElement("startTransaction", BsonBoolean.TRUE));
286286
addReadConcernDocument(extraElements, sessionContext);
287-
if (serverApi != null) {
288-
addServerApiElements(extraElements);
289-
}
290287
}
291288
extraElements.add(new BsonElement("autocommit", BsonBoolean.FALSE));
292-
} else if (serverApi != null) {
289+
}
290+
291+
if (serverApi != null) {
293292
addServerApiElements(extraElements);
294293
}
294+
295295
if (readPreference != null) {
296296
if (!readPreference.equals(primary())) {
297297
extraElements.add(new BsonElement("$readPreference", readPreference.toDocument()));
@@ -333,7 +333,7 @@ private static OpCode getOpCode(final MessageSettings settings) {
333333
}
334334

335335
private static boolean isServerVersionAtLeastThreeDotSix(final MessageSettings settings) {
336-
return settings.getMaxWireVersion() >= THREE_DOT_SIX_WIRE_VERSION;
336+
return settings.getMaxWireVersion() >= THREE_DOT_SIX_WIRE_VERSION;
337337
}
338338

339339
}

driver-core/src/main/com/mongodb/internal/operation/AsyncQueryBatchCursor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private void getMore(final AsyncConnection connection, final ServerCursor cursor
247247
if (serverIsAtLeastVersionThreeDotTwo(connection.getDescription())) {
248248
connection.commandAsync(namespace.getDatabaseName(), asGetMoreCommandDocument(cursor.getId()), NO_OP_FIELD_NAME_VALIDATOR,
249249
ReadPreference.primary(), CommandResultDocumentCodec.create(decoder, "nextBatch"),
250-
connectionSource.getSessionContext(), null /* spec requires no server api for getMore */,
250+
connectionSource.getSessionContext(), connectionSource.getServerApi(),
251251
new CommandResultSingleResultCallback(connection, cursor, callback, tryNext));
252252

253253
} else {

driver-core/src/main/com/mongodb/internal/operation/QueryBatchCursor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ private void getMore() {
273273
ReadPreference.primary(),
274274
CommandResultDocumentCodec.create(decoder, "nextBatch"),
275275
connectionSource.getSessionContext(),
276-
null /* As per spec, ServerApi elements are not included in getMore commands */));
276+
connectionSource.getServerApi()));
277277
} catch (MongoCommandException e) {
278278
throw translateCommandException(e, serverCursor);
279279
}

driver-core/src/test/resources/versioned-api/crud-api-version-1-strict.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@
651651
]
652652
},
653653
{
654-
"description": "find command with declared API version appends to the command, but getMore does not",
654+
"description": "find and getMore append API version",
655655
"operations": [
656656
{
657657
"name": "find",
@@ -712,14 +712,10 @@
712712
"long"
713713
]
714714
},
715-
"apiVersion": {
716-
"$$exists": false
717-
},
718-
"apiStrict": {
719-
"$$exists": false
720-
},
715+
"apiVersion": "1",
716+
"apiStrict": true,
721717
"apiDeprecationErrors": {
722-
"$$exists": false
718+
"$$unsetOrMatches": false
723719
}
724720
}
725721
}

driver-core/src/test/resources/versioned-api/crud-api-version-1.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@
643643
]
644644
},
645645
{
646-
"description": "find command with declared API version appends to the command, but getMore does not",
646+
"description": "find and getMore append API version",
647647
"operations": [
648648
{
649649
"name": "find",
@@ -704,15 +704,11 @@
704704
"long"
705705
]
706706
},
707-
"apiVersion": {
708-
"$$exists": false
709-
},
707+
"apiVersion": "1",
710708
"apiStrict": {
711-
"$$exists": false
709+
"$$unsetOrMatches": false
712710
},
713-
"apiDeprecationErrors": {
714-
"$$exists": false
715-
}
711+
"apiDeprecationErrors": true
716712
}
717713
}
718714
}

driver-core/src/test/resources/versioned-api/transaction-handling.json

Lines changed: 11 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@
5353
"apiDeprecationErrors": {
5454
"$$unsetOrMatches": false
5555
}
56-
},
57-
{
58-
"apiVersion": {
59-
"$$exists": false
60-
},
61-
"apiStrict": {
62-
"$$exists": false
63-
},
64-
"apiDeprecationErrors": {
65-
"$$exists": false
66-
}
6756
}
6857
]
6958
},
@@ -97,7 +86,7 @@
9786
],
9887
"tests": [
9988
{
100-
"description": "Only the first command in a transaction declares an API version",
89+
"description": "All commands in a transaction declare an API version",
10190
"runOnRequirements": [
10291
{
10392
"topologies": [
@@ -193,119 +182,6 @@
193182
"lsid": {
194183
"$$sessionLsid": "session"
195184
},
196-
"apiVersion": {
197-
"$$exists": false
198-
},
199-
"apiStrict": {
200-
"$$exists": false
201-
},
202-
"apiDeprecationErrors": {
203-
"$$exists": false
204-
}
205-
}
206-
}
207-
},
208-
{
209-
"commandStartedEvent": {
210-
"command": {
211-
"commitTransaction": 1,
212-
"lsid": {
213-
"$$sessionLsid": "session"
214-
},
215-
"apiVersion": {
216-
"$$exists": false
217-
},
218-
"apiStrict": {
219-
"$$exists": false
220-
},
221-
"apiDeprecationErrors": {
222-
"$$exists": false
223-
}
224-
}
225-
}
226-
}
227-
]
228-
}
229-
]
230-
},
231-
{
232-
"description": "Committing a transaction twice does not append server API options",
233-
"runOnRequirements": [
234-
{
235-
"topologies": [
236-
"replicaset",
237-
"sharded-replicaset"
238-
]
239-
}
240-
],
241-
"operations": [
242-
{
243-
"name": "startTransaction",
244-
"object": "session"
245-
},
246-
{
247-
"name": "insertOne",
248-
"object": "collection",
249-
"arguments": {
250-
"session": "session",
251-
"document": {
252-
"_id": 6,
253-
"x": 66
254-
}
255-
},
256-
"expectResult": {
257-
"$$unsetOrMatches": {
258-
"insertedId": {
259-
"$$unsetOrMatches": 6
260-
}
261-
}
262-
}
263-
},
264-
{
265-
"name": "insertOne",
266-
"object": "collection",
267-
"arguments": {
268-
"session": "session",
269-
"document": {
270-
"_id": 7,
271-
"x": 77
272-
}
273-
},
274-
"expectResult": {
275-
"$$unsetOrMatches": {
276-
"insertedId": {
277-
"$$unsetOrMatches": 7
278-
}
279-
}
280-
}
281-
},
282-
{
283-
"name": "commitTransaction",
284-
"object": "session"
285-
},
286-
{
287-
"name": "commitTransaction",
288-
"object": "session"
289-
}
290-
],
291-
"expectEvents": [
292-
{
293-
"client": "client",
294-
"events": [
295-
{
296-
"commandStartedEvent": {
297-
"command": {
298-
"insert": "test",
299-
"documents": [
300-
{
301-
"_id": 6,
302-
"x": 66
303-
}
304-
],
305-
"lsid": {
306-
"$$sessionLsid": "session"
307-
},
308-
"startTransaction": true,
309185
"apiVersion": "1",
310186
"apiStrict": {
311187
"$$unsetOrMatches": false
@@ -316,65 +192,19 @@
316192
}
317193
}
318194
},
319-
{
320-
"commandStartedEvent": {
321-
"command": {
322-
"insert": "test",
323-
"documents": [
324-
{
325-
"_id": 7,
326-
"x": 77
327-
}
328-
],
329-
"lsid": {
330-
"$$sessionLsid": "session"
331-
},
332-
"apiVersion": {
333-
"$$exists": false
334-
},
335-
"apiStrict": {
336-
"$$exists": false
337-
},
338-
"apiDeprecationErrors": {
339-
"$$exists": false
340-
}
341-
}
342-
}
343-
},
344195
{
345196
"commandStartedEvent": {
346197
"command": {
347198
"commitTransaction": 1,
348199
"lsid": {
349200
"$$sessionLsid": "session"
350201
},
351-
"apiVersion": {
352-
"$$exists": false
353-
},
354-
"apiStrict": {
355-
"$$exists": false
356-
},
357-
"apiDeprecationErrors": {
358-
"$$exists": false
359-
}
360-
}
361-
}
362-
},
363-
{
364-
"commandStartedEvent": {
365-
"command": {
366-
"commitTransaction": 1,
367-
"lsid": {
368-
"$$sessionLsid": "session"
369-
},
370-
"apiVersion": {
371-
"$$exists": false
372-
},
202+
"apiVersion": "1",
373203
"apiStrict": {
374-
"$$exists": false
204+
"$$unsetOrMatches": false
375205
},
376206
"apiDeprecationErrors": {
377-
"$$exists": false
207+
"$$unsetOrMatches": false
378208
}
379209
}
380210
}
@@ -384,7 +214,7 @@
384214
]
385215
},
386216
{
387-
"description": "abortTransaction does not include an API version",
217+
"description": "abortTransaction includes an API version",
388218
"runOnRequirements": [
389219
{
390220
"topologies": [
@@ -480,14 +310,12 @@
480310
"lsid": {
481311
"$$sessionLsid": "session"
482312
},
483-
"apiVersion": {
484-
"$$exists": false
485-
},
313+
"apiVersion": "1",
486314
"apiStrict": {
487-
"$$exists": false
315+
"$$unsetOrMatches": false
488316
},
489317
"apiDeprecationErrors": {
490-
"$$exists": false
318+
"$$unsetOrMatches": false
491319
}
492320
}
493321
}
@@ -499,14 +327,12 @@
499327
"lsid": {
500328
"$$sessionLsid": "session"
501329
},
502-
"apiVersion": {
503-
"$$exists": false
504-
},
330+
"apiVersion": "1",
505331
"apiStrict": {
506-
"$$exists": false
332+
"$$unsetOrMatches": false
507333
},
508334
"apiDeprecationErrors": {
509-
"$$exists": false
335+
"$$unsetOrMatches": false
510336
}
511337
}
512338
}

0 commit comments

Comments
 (0)