diff --git a/mongo/options/clientoptions.go b/mongo/options/clientoptions.go index 007d235390..9623562ec8 100644 --- a/mongo/options/clientoptions.go +++ b/mongo/options/clientoptions.go @@ -1265,6 +1265,10 @@ func extractX509UsernameFromSubject(subject string) string { // precedence. func MergeClientOptions(opts ...*ClientOptions) *ClientOptions { if len(opts) == 1 { + if opts[0] == nil { + return Client() + } + return opts[0] } diff --git a/mongo/options/clientoptions_test.go b/mongo/options/clientoptions_test.go index 61bfa7dd0a..6a384e660f 100644 --- a/mongo/options/clientoptions_test.go +++ b/mongo/options/clientoptions_test.go @@ -175,6 +175,16 @@ func TestClientOptions(t *testing.T) { t.Errorf("Merged client options do not match. got %v; want %v", got.err.Error(), opt1.err.Error()) } }) + + t.Run("MergeClientOptions single nil option", func(t *testing.T) { + got := MergeClientOptions(nil) + assert.Equal(t, Client(), got) + }) + + t.Run("MergeClientOptions multiple nil options", func(t *testing.T) { + got := MergeClientOptions(nil, nil) + assert.Equal(t, Client(), got) + }) }) t.Run("direct connection validation", func(t *testing.T) { t.Run("multiple hosts", func(t *testing.T) {