Skip to content

Commit b1763db

Browse files
committed
GODRIVER-2205 Retain empty tag sets when building a read preference document. (#787)
1 parent 9002947 commit b1763db

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

x/mongo/driver/operation.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,9 +1233,6 @@ func (op Operation) createReadPref(serverKind description.ServerKind, topologyKi
12331233

12341234
sets := make([]bsoncore.Document, 0, len(rp.TagSets()))
12351235
for _, ts := range rp.TagSets() {
1236-
if len(ts) == 0 {
1237-
continue
1238-
}
12391236
i, set := bsoncore.AppendDocumentStart(nil)
12401237
for _, t := range ts {
12411238
set = bsoncore.AppendStringElement(set, t.Name, t.Value)

x/mongo/driver/operation_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"go.mongodb.org/mongo-driver/mongo/readconcern"
1717
"go.mongodb.org/mongo-driver/mongo/readpref"
1818
"go.mongodb.org/mongo-driver/mongo/writeconcern"
19+
"go.mongodb.org/mongo-driver/tag"
1920
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2021
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
2122
"go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
@@ -397,6 +398,25 @@ func TestOperation(t *testing.T) {
397398
readpref.SecondaryPreferred(readpref.WithTags("disk", "ssd", "use", "reporting")),
398399
description.RSSecondary, description.ReplicaSet, false, rpWithTags,
399400
},
401+
// GODRIVER-2205: Ensure empty tag sets are written as an empty document in the read
402+
// preference document. Empty tag sets match any server and are used as a fallback when
403+
// no other tag sets match any servers.
404+
{
405+
"secondaryPreferred/withTags/emptyTagSet",
406+
readpref.SecondaryPreferred(readpref.WithTagSets(
407+
tag.Set{{Name: "disk", Value: "ssd"}},
408+
tag.Set{})),
409+
description.RSSecondary,
410+
description.ReplicaSet,
411+
false,
412+
bsoncore.NewDocumentBuilder().
413+
AppendString("mode", "secondaryPreferred").
414+
AppendArray("tags", bsoncore.NewArrayBuilder().
415+
AppendDocument(bsoncore.NewDocumentBuilder().AppendString("disk", "ssd").Build()).
416+
AppendDocument(bsoncore.NewDocumentBuilder().Build()).
417+
Build()).
418+
Build(),
419+
},
400420
{
401421
"secondaryPreferred/withMaxStaleness",
402422
readpref.SecondaryPreferred(readpref.WithMaxStaleness(25 * time.Second)),

0 commit comments

Comments
 (0)