Skip to content

Commit

Permalink
GODRIVER-2804 Fix bug that can squash FullDocument when merging Chang…
Browse files Browse the repository at this point in the history
…eStreamOptions. (#1225)

Co-authored-by: Preston Vasquez <prestonvasquez@icloud.com>
  • Loading branch information
2 people authored and qingyang-hu committed May 2, 2023
1 parent d8040b4 commit 374e369
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
6 changes: 2 additions & 4 deletions mongo/change_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,8 @@ func (cs *ChangeStream) createPipelineOptionsDoc() (bsoncore.Document, error) {
plDoc = bsoncore.AppendBooleanElement(plDoc, "allChangesForCluster", true)
}

if cs.options.FullDocument != nil {
if *cs.options.FullDocument != options.Default {
plDoc = bsoncore.AppendStringElement(plDoc, "fullDocument", string(*cs.options.FullDocument))
}
if cs.options.FullDocument != nil && *cs.options.FullDocument != options.Default {
plDoc = bsoncore.AppendStringElement(plDoc, "fullDocument", string(*cs.options.FullDocument))
}

if cs.options.FullDocumentBeforeChange != nil {
Expand Down
1 change: 0 additions & 1 deletion mongo/options/changestreamoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ type ChangeStreamOptions struct {
// ChangeStream creates a new ChangeStreamOptions instance.
func ChangeStream() *ChangeStreamOptions {
cso := &ChangeStreamOptions{}
cso.SetFullDocument(Default)
return cso
}

Expand Down
64 changes: 64 additions & 0 deletions mongo/options/changestreamoptions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package options

import (
"testing"

"go.mongodb.org/mongo-driver/internal/assert"
)

func TestMergeChangeStreamOptions(t *testing.T) {
t.Parallel()

fullDocumentP := func(x FullDocument) *FullDocument { return &x }
int32P := func(x int32) *int32 { return &x }

testCases := []struct {
description string
input []*ChangeStreamOptions
want *ChangeStreamOptions
}{
{
description: "empty",
input: []*ChangeStreamOptions{},
want: &ChangeStreamOptions{},
},
{
description: "many ChangeStreamOptions with one configuration each",
input: []*ChangeStreamOptions{
ChangeStream().SetFullDocumentBeforeChange(Required),
ChangeStream().SetFullDocument(Required),
ChangeStream().SetBatchSize(10),
},
want: &ChangeStreamOptions{
FullDocument: fullDocumentP(Required),
FullDocumentBeforeChange: fullDocumentP(Required),
BatchSize: int32P(10),
},
},
{
description: "single ChangeStreamOptions with many configurations",
input: []*ChangeStreamOptions{
ChangeStream().
SetFullDocumentBeforeChange(Required).
SetFullDocument(Required).
SetBatchSize(10),
},
want: &ChangeStreamOptions{
FullDocument: fullDocumentP(Required),
FullDocumentBeforeChange: fullDocumentP(Required),
BatchSize: int32P(10),
},
},
}

for _, tc := range testCases {
tc := tc // Capture range variable.

t.Run(tc.description, func(t *testing.T) {
t.Parallel()

got := MergeChangeStreamOptions(tc.input...)
assert.Equal(t, tc.want, got, "expected and actual ChangeStreamOptions are different")
})
}
}

0 comments on commit 374e369

Please sign in to comment.