Skip to content

Commit 374e369

Browse files
matthewdaleprestonvasquez
authored andcommitted
GODRIVER-2804 Fix bug that can squash FullDocument when merging ChangeStreamOptions. (#1225)
Co-authored-by: Preston Vasquez <[email protected]>
1 parent d8040b4 commit 374e369

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

mongo/change_stream.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,8 @@ func (cs *ChangeStream) createPipelineOptionsDoc() (bsoncore.Document, error) {
428428
plDoc = bsoncore.AppendBooleanElement(plDoc, "allChangesForCluster", true)
429429
}
430430

431-
if cs.options.FullDocument != nil {
432-
if *cs.options.FullDocument != options.Default {
433-
plDoc = bsoncore.AppendStringElement(plDoc, "fullDocument", string(*cs.options.FullDocument))
434-
}
431+
if cs.options.FullDocument != nil && *cs.options.FullDocument != options.Default {
432+
plDoc = bsoncore.AppendStringElement(plDoc, "fullDocument", string(*cs.options.FullDocument))
435433
}
436434

437435
if cs.options.FullDocumentBeforeChange != nil {

mongo/options/changestreamoptions.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ type ChangeStreamOptions struct {
7474
// ChangeStream creates a new ChangeStreamOptions instance.
7575
func ChangeStream() *ChangeStreamOptions {
7676
cso := &ChangeStreamOptions{}
77-
cso.SetFullDocument(Default)
7877
return cso
7978
}
8079

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package options
2+
3+
import (
4+
"testing"
5+
6+
"go.mongodb.org/mongo-driver/internal/assert"
7+
)
8+
9+
func TestMergeChangeStreamOptions(t *testing.T) {
10+
t.Parallel()
11+
12+
fullDocumentP := func(x FullDocument) *FullDocument { return &x }
13+
int32P := func(x int32) *int32 { return &x }
14+
15+
testCases := []struct {
16+
description string
17+
input []*ChangeStreamOptions
18+
want *ChangeStreamOptions
19+
}{
20+
{
21+
description: "empty",
22+
input: []*ChangeStreamOptions{},
23+
want: &ChangeStreamOptions{},
24+
},
25+
{
26+
description: "many ChangeStreamOptions with one configuration each",
27+
input: []*ChangeStreamOptions{
28+
ChangeStream().SetFullDocumentBeforeChange(Required),
29+
ChangeStream().SetFullDocument(Required),
30+
ChangeStream().SetBatchSize(10),
31+
},
32+
want: &ChangeStreamOptions{
33+
FullDocument: fullDocumentP(Required),
34+
FullDocumentBeforeChange: fullDocumentP(Required),
35+
BatchSize: int32P(10),
36+
},
37+
},
38+
{
39+
description: "single ChangeStreamOptions with many configurations",
40+
input: []*ChangeStreamOptions{
41+
ChangeStream().
42+
SetFullDocumentBeforeChange(Required).
43+
SetFullDocument(Required).
44+
SetBatchSize(10),
45+
},
46+
want: &ChangeStreamOptions{
47+
FullDocument: fullDocumentP(Required),
48+
FullDocumentBeforeChange: fullDocumentP(Required),
49+
BatchSize: int32P(10),
50+
},
51+
},
52+
}
53+
54+
for _, tc := range testCases {
55+
tc := tc // Capture range variable.
56+
57+
t.Run(tc.description, func(t *testing.T) {
58+
t.Parallel()
59+
60+
got := MergeChangeStreamOptions(tc.input...)
61+
assert.Equal(t, tc.want, got, "expected and actual ChangeStreamOptions are different")
62+
})
63+
}
64+
}

0 commit comments

Comments
 (0)