Skip to content

Commit 901fb64

Browse files
authored
Merge branch 'master' into godriver2775-replaceerrors
2 parents ab25c65 + 32c7b85 commit 901fb64

File tree

21 files changed

+189
-336
lines changed

21 files changed

+189
-336
lines changed

bson/doc.go

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,14 @@
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66

7-
// Package bson is a library for reading, writing, and manipulating BSON. BSON is a binary serialization format used to
8-
// store documents and make remote procedure calls in MongoDB. The BSON specification is located at https://bsonspec.org.
9-
// The BSON library handles marshaling and unmarshaling of values through a configurable codec system. For a description
10-
// of the codec system and examples of registering custom codecs, see the bsoncodec package. For additional information
11-
// and usage examples, check out the [Work with BSON] page in the Go Driver docs site.
12-
//
13-
// # Raw BSON
14-
//
15-
// The Raw family of types is used to validate and retrieve elements from a slice of bytes. This
16-
// type is most useful when you want do lookups on BSON bytes without unmarshaling it into another
17-
// type.
18-
//
19-
// Example:
20-
//
21-
// var raw bson.Raw = ... // bytes from somewhere
22-
// err := raw.Validate()
23-
// if err != nil { return err }
24-
// val := raw.Lookup("foo")
25-
// i32, ok := val.Int32OK()
26-
// // do something with i32...
7+
// Package bson is a library for reading, writing, and manipulating BSON. BSON is a binary serialization
8+
// format used to store documents and make remote procedure calls in MongoDB. For more information about
9+
// the Go BSON library, including usage examples, check out the [Work with BSON] page in the Go Driver
10+
// docs site. For more information about BSON, see https://bsonspec.org.
2711
//
2812
// # Native Go Types
2913
//
30-
// The D and M types defined in this package can be used to build representations of BSON using native Go types. D is a
14+
// The [D] and [M] types defined in this package can be used to build representations of BSON using native Go types. D is a
3115
// slice and M is a map. For more information about the use cases for these types, see the documentation on the type
3216
// definitions.
3317
//
@@ -93,26 +77,16 @@
9377
// 5. When unmarshaling, a field of type interface{} will follow the D/M type mappings listed above. BSON documents
9478
// unmarshaled into an interface{} field will be unmarshaled as a D.
9579
//
96-
// The encoding of each struct field can be customized by the "bson" struct tag.
97-
//
98-
// This tag behavior is configurable, and different struct tag behavior can be configured by initializing a new
99-
// bsoncodec.StructCodec with the desired tag parser and registering that StructCodec onto the Registry. By default, JSON
100-
// tags are not honored, but that can be enabled by creating a StructCodec with JSONFallbackStructTagParser, like below:
101-
//
102-
// Example:
103-
//
104-
// structcodec, _ := bsoncodec.NewStructCodec(bsoncodec.JSONFallbackStructTagParser)
105-
//
106-
// The bson tag gives the name of the field, possibly followed by a comma-separated list of options.
107-
// The name may be empty in order to specify options without overriding the default field name. The following options can
108-
// be used to configure behavior:
80+
// The encoding of each struct field can be customized by the "bson" struct tag. The "bson" tag gives the name of the
81+
// field, followed by a comma-separated list of options. The name may be omitted in order to specify options without
82+
// overriding the default field name. The following options can be used to configure behavior:
10983
//
11084
// 1. omitempty: If the "omitempty" struct tag is specified on a field, the field will not be marshaled if it is set to
11185
// an "empty" value. Numbers, booleans, and strings are considered empty if their value is equal to the zero value for
11286
// the type (i.e. 0 for numbers, false for booleans, and "" for strings). Slices, maps, and arrays are considered
11387
// empty if they are of length zero. Interfaces and pointers are considered empty if their value is nil. By default,
114-
// structs are only considered empty if the struct type implements [bsoncodec.Zeroer] and the IsZero
115-
// method returns true. Struct types that do not implement [bsoncodec.Zeroer] are never considered empty and will be
88+
// structs are only considered empty if the struct type implements [Zeroer] and the "IsZero"
89+
// method returns true. Struct types that do not implement [Zeroer] are never considered empty and will be
11690
// marshaled as embedded documents. NOTE: It is recommended that this tag be used for all slice and map fields.
11791
//
11892
// 2. minsize: If the minsize struct tag is specified on a field of type int64, uint, uint32, or uint64 and the value of
@@ -134,22 +108,34 @@
134108
// error will be returned. This tag can be used with fields that are pointers to structs. If an inlined pointer field
135109
// is nil, it will not be marshaled. For fields that are not maps or structs, this tag is ignored.
136110
//
137-
// # Marshaling and Unmarshaling
111+
// # Raw BSON
138112
//
139-
// Manually marshaling and unmarshaling can be done with the Marshal and Unmarshal family of functions.
113+
// The Raw family of types is used to validate and retrieve elements from a slice of bytes. This
114+
// type is most useful when you want do lookups on BSON bytes without unmarshaling it into another
115+
// type.
140116
//
141-
// bsoncodec code provides a system for encoding values to BSON representations and decoding
142-
// values from BSON representations. This package considers both binary BSON and ExtendedJSON as
143-
// BSON representations. The types in this package enable a flexible system for handling this
144-
// encoding and decoding.
117+
// Example:
145118
//
146-
// The codec system is composed of two parts:
119+
// var raw bson.Raw = ... // bytes from somewhere
120+
// err := raw.Validate()
121+
// if err != nil { return err }
122+
// val := raw.Lookup("foo")
123+
// i32, ok := val.Int32OK()
124+
// // do something with i32...
125+
//
126+
// # Custom Registry
127+
//
128+
// The Go BSON library uses a [Registry] to define encoding and decoding behavior for different data types.
129+
// The default encoding and decoding behavior can be customized or extended by using a modified Registry.
130+
// The custom registry system is composed of two parts:
147131
//
148132
// 1) [ValueEncoder] and [ValueDecoder] that handle encoding and decoding Go values to and from BSON
149133
// representations.
150134
//
151135
// 2) A [Registry] that holds these ValueEncoders and ValueDecoders and provides methods for
152136
// retrieving them.
153137
//
138+
// To use a custom Registry, use [Encoder.SetRegistry] or [Decoder.SetRegistry].
139+
//
154140
// [Work with BSON]: https://www.mongodb.com/docs/drivers/go/current/fundamentals/bson/
155141
package bson

internal/httputil/httputil.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ import (
1010
"net/http"
1111
)
1212

13-
// DefaultHTTPClient is the default HTTP client used across the driver.
14-
var DefaultHTTPClient = &http.Client{
15-
Transport: http.DefaultTransport.(*http.Transport).Clone(),
13+
var DefaultHTTPClient = &http.Client{}
14+
15+
// NewHTTPClient will return the globally-defined DefaultHTTPClient, updating
16+
// the transport if it differs from the http package DefaultTransport.
17+
func NewHTTPClient() *http.Client {
18+
client := DefaultHTTPClient
19+
if _, ok := http.DefaultTransport.(*http.Transport); !ok {
20+
client.Transport = http.DefaultTransport
21+
}
22+
23+
return client
1624
}
1725

1826
// CloseIdleHTTPConnections closes any connections which were previously

internal/httputil/httputil_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (C) MongoDB, Inc. 2022-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
package httputil
8+
9+
import (
10+
"net/http"
11+
"testing"
12+
13+
"go.mongodb.org/mongo-driver/v2/internal/assert"
14+
)
15+
16+
type nonDefaultTransport struct{}
17+
18+
func (*nonDefaultTransport) RoundTrip(*http.Request) (*http.Response, error) { return nil, nil }
19+
20+
func TestDefaultHTTPClientTransport(t *testing.T) {
21+
t.Run("default", func(t *testing.T) {
22+
client := NewHTTPClient()
23+
24+
val := assert.ObjectsAreEqual(http.DefaultClient, client)
25+
26+
assert.True(t, val)
27+
assert.Equal(t, DefaultHTTPClient, client)
28+
})
29+
30+
t.Run("non-default global transport", func(t *testing.T) {
31+
http.DefaultTransport = &nonDefaultTransport{}
32+
33+
client := NewHTTPClient()
34+
35+
val := assert.ObjectsAreEqual(&nonDefaultTransport{}, client.Transport)
36+
37+
assert.True(t, val)
38+
assert.Equal(t, DefaultHTTPClient, client)
39+
assert.NotEqual(t, http.DefaultClient, client) // Sanity Check
40+
})
41+
}

internal/integtest/integtest.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ func AddServerlessAuthCredentials(uri string) (string, error) {
223223

224224
// ConnString gets the globally configured connection string.
225225
func ConnString(t *testing.T) *connstring.ConnString {
226+
if testing.Short() {
227+
t.Skip("skipping integration test in short mode")
228+
}
229+
226230
connectionStringOnce.Do(func() {
227231
uri, err := MongoDBURI()
228232
require.NoError(t, err, "error constructing mongodb URI: %v", err)

internal/test/aws/aws_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020

2121
func TestAWS(t *testing.T) {
2222
uri := os.Getenv("MONGODB_URI")
23+
if uri == "" {
24+
t.Skip("Skipping test: MONGODB_URI environment variable is not set")
25+
}
2326

2427
client, err := mongo.Connect(options.Client().ApplyURI(uri))
25-
require.NoError(t, err, "Connect error")
2628

2729
defer func() {
2830
err = client.Disconnect(context.Background())

mongo/options/clientoptions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type ContextDialer interface {
6868
// Credential can be used to provide authentication options when configuring a Client.
6969
//
7070
// AuthMechanism: the mechanism to use for authentication. Supported values include "SCRAM-SHA-256", "SCRAM-SHA-1",
71-
// "MONGODB-CR", "PLAIN", "GSSAPI", "MONGODB-X509", and "MONGODB-AWS". This can also be set through the "authMechanism"
71+
// "PLAIN", "GSSAPI", "MONGODB-X509", and "MONGODB-AWS". This can also be set through the "authMechanism"
7272
// URI option. (e.g. "authMechanism=PLAIN"). For more information, see
7373
// https://www.mongodb.com/docs/manual/core/authentication-mechanisms/.
7474
//
@@ -311,7 +311,7 @@ type ClientOptions struct {
311311
// Client creates a new ClientOptions instance.
312312
func Client() *ClientOptions {
313313
opts := &ClientOptions{}
314-
opts = opts.SetHTTPClient(httputil.DefaultHTTPClient)
314+
opts = opts.SetHTTPClient(httputil.NewHTTPClient())
315315

316316
return opts
317317
}

mongo/options/clientoptions_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,22 @@ func TestClientOptions(t *testing.T) {
527527
})
528528
}
529529

530+
type nonDefaultTransport struct{}
531+
532+
func (*nonDefaultTransport) RoundTrip(*http.Request) (*http.Response, error) { return nil, nil }
533+
534+
func TestClientHTTPTransport(t *testing.T) {
535+
t.Run("Default client", func(t *testing.T) {
536+
got := Client().HTTPClient
537+
assert.Equal(t, http.DefaultClient, got)
538+
})
539+
t.Run("Non-default global transport", func(t *testing.T) {
540+
http.DefaultTransport = &nonDefaultTransport{}
541+
got := Client().HTTPClient.Transport
542+
assert.Equal(t, &nonDefaultTransport{}, got)
543+
})
544+
}
545+
530546
func createCertPool(t *testing.T, paths ...string) *x509.CertPool {
531547
t.Helper()
532548

testdata/specifications

Submodule specifications updated 39 files

x/mongo/driver/auth/auth.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"net/http"
14+
"strings"
1415

1516
"go.mongodb.org/mongo-driver/v2/mongo/address"
1617
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
@@ -31,7 +32,6 @@ func init() {
3132
RegisterAuthenticatorFactory("", newDefaultAuthenticator)
3233
RegisterAuthenticatorFactory(SCRAMSHA1, newScramSHA1Authenticator)
3334
RegisterAuthenticatorFactory(SCRAMSHA256, newScramSHA256Authenticator)
34-
RegisterAuthenticatorFactory(MONGODBCR, newMongoDBCRAuthenticator)
3535
RegisterAuthenticatorFactory(PLAIN, newPlainAuthenticator)
3636
RegisterAuthenticatorFactory(GSSAPI, newGSSAPIAuthenticator)
3737
RegisterAuthenticatorFactory(MongoDBX509, newMongoDBX509Authenticator)
@@ -41,6 +41,12 @@ func init() {
4141

4242
// CreateAuthenticator creates an authenticator.
4343
func CreateAuthenticator(name string, cred *Cred, httpClient *http.Client) (Authenticator, error) {
44+
// Return a custom error to indicate why auth mechanism "MONGODB-CR" is
45+
// missing, even though it was previously available.
46+
if strings.EqualFold(name, "MONGODB-CR") {
47+
return nil, errors.New(`auth mechanism "MONGODB-CR" is no longer available in any supported version of MongoDB`)
48+
}
49+
4450
if f, ok := authFactories[name]; ok {
4551
return f(cred, httpClient)
4652
}

x/mongo/driver/auth/auth_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package auth_test
88

99
import (
1010
"context"
11+
"errors"
1112
"fmt"
1213
"net/http"
1314
"testing"
@@ -29,11 +30,12 @@ func TestCreateAuthenticator(t *testing.T) {
2930
name string
3031
source string
3132
auth auth.Authenticator
33+
err error
3234
}{
3335
{name: "", auth: &auth.DefaultAuthenticator{}},
3436
{name: "SCRAM-SHA-1", auth: &auth.ScramAuthenticator{}},
3537
{name: "SCRAM-SHA-256", auth: &auth.ScramAuthenticator{}},
36-
{name: "MONGODB-CR", auth: &auth.MongoDBCRAuthenticator{}},
38+
{name: "MONGODB-CR", err: errors.New(`auth mechanism "MONGODB-CR" is no longer available in any supported version of MongoDB`)},
3739
{name: "PLAIN", auth: &auth.PlainAuthenticator{}},
3840
{name: "MONGODB-X509", auth: &auth.MongoDBX509Authenticator{}},
3941
}
@@ -47,6 +49,10 @@ func TestCreateAuthenticator(t *testing.T) {
4749
}
4850

4951
a, err := auth.CreateAuthenticator(test.name, cred, &http.Client{})
52+
if test.err != nil {
53+
require.EqualError(t, err, test.err.Error())
54+
return
55+
}
5056
require.NoError(t, err)
5157
require.IsType(t, test.auth, a)
5258
})

0 commit comments

Comments
 (0)