From 9eb9e37afe1a60c8db00b319e9adce0655c0717d Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Wed, 15 Jan 2025 09:34:30 -0700 Subject: [PATCH] GODRIVER-3452 MergeClientOptions returns object when given nil arguments (#1917) --- mongo/options/clientoptions.go | 4 ++++ mongo/options/clientoptions_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+) 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) {