From 376087a41ffd46fe0d713f087a5aadf2d8dac9fe Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Thu, 5 Dec 2024 14:16:42 -0700 Subject: [PATCH 1/4] GODRIVER-3436 Avoid initializing null data given custom decoder --- bson/bsoncodec/default_value_decoders.go | 6 ++++++ bson/unmarshaling_cases_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/bson/bsoncodec/default_value_decoders.go b/bson/bsoncodec/default_value_decoders.go index fc4a7b1dbf..159297ef0a 100644 --- a/bson/bsoncodec/default_value_decoders.go +++ b/bson/bsoncodec/default_value_decoders.go @@ -1521,6 +1521,12 @@ func (dvd DefaultValueDecoders) ValueUnmarshalerDecodeValue(_ DecodeContext, vr return ValueDecoderError{Name: "ValueUnmarshalerDecodeValue", Types: []reflect.Type{tValueUnmarshaler}, Received: val} } + if vr.Type() == bsontype.Null { + val.Set(reflect.Zero(val.Type())) + + return vr.ReadNull() + } + if val.Kind() == reflect.Ptr && val.IsNil() { if !val.CanSet() { return ValueDecoderError{Name: "ValueUnmarshalerDecodeValue", Types: []reflect.Type{tValueUnmarshaler}, Received: val} diff --git a/bson/unmarshaling_cases_test.go b/bson/unmarshaling_cases_test.go index 37d9ded318..dd7405547d 100644 --- a/bson/unmarshaling_cases_test.go +++ b/bson/unmarshaling_cases_test.go @@ -11,6 +11,7 @@ import ( "go.mongodb.org/mongo-driver/bson/bsonrw" "go.mongodb.org/mongo-driver/bson/bsontype" + "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" ) type unmarshalingTestCase struct { @@ -174,6 +175,12 @@ func unmarshalingTestCases() []unmarshalingTestCase { want: &valNonPtrStruct, data: docToBytes(valNonPtrStruct), }, + { + name: "meep", + sType: reflect.TypeOf(unmarshalerPtrStruct{}), + want: &unmarshalerPtrStruct{}, + data: docWithNullValueBytes("I"), + }, } } @@ -250,3 +257,14 @@ func (ms *myString) UnmarshalBSON(bytes []byte) error { *ms = myString(s) return nil } + +// create a byte slice that represents BSON with a variable key value that is +// null, e.g. {: null}. +func docWithNullValueBytes(key string) []byte { + idx, doc := bsoncore.AppendDocumentStart(nil) + doc = bsoncore.AppendNullElement(doc, key) + + doc, _ = bsoncore.AppendDocumentEnd(doc, idx) + + return Raw(doc) +} From 101fb6d211b5e09a668a0f990b7f1368a9350219 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Thu, 5 Dec 2024 14:27:54 -0700 Subject: [PATCH 2/4] GODRIVER-3436 Update test name --- bson/unmarshaling_cases_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bson/unmarshaling_cases_test.go b/bson/unmarshaling_cases_test.go index dd7405547d..d78db897ca 100644 --- a/bson/unmarshaling_cases_test.go +++ b/bson/unmarshaling_cases_test.go @@ -176,7 +176,7 @@ func unmarshalingTestCases() []unmarshalingTestCase { data: docToBytes(valNonPtrStruct), }, { - name: "meep", + name: "do not initialize null-literal data for custom type and unmarshaler", sType: reflect.TypeOf(unmarshalerPtrStruct{}), want: &unmarshalerPtrStruct{}, data: docWithNullValueBytes("I"), From 080bf13787be9ae51cfef41d84d86d9803db0d10 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Thu, 5 Dec 2024 14:29:19 -0700 Subject: [PATCH 3/4] GODRIVER-3436 Update test name --- bson/unmarshaling_cases_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bson/unmarshaling_cases_test.go b/bson/unmarshaling_cases_test.go index d78db897ca..14049d63f7 100644 --- a/bson/unmarshaling_cases_test.go +++ b/bson/unmarshaling_cases_test.go @@ -176,7 +176,7 @@ func unmarshalingTestCases() []unmarshalingTestCase { data: docToBytes(valNonPtrStruct), }, { - name: "do not initialize null-literal data for custom type and unmarshaler", + name: "null-literal data for custom type and unmarshaler should not be initialize", sType: reflect.TypeOf(unmarshalerPtrStruct{}), want: &unmarshalerPtrStruct{}, data: docWithNullValueBytes("I"), From 8413567800843a08a7d87646acd68ef6b3331572 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Thu, 5 Dec 2024 15:15:38 -0700 Subject: [PATCH 4/4] GODRIVER-3436 Use ValueUnmarshaler for test --- bson/unmarshaling_cases_test.go | 37 +++++++++++++++++---------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/bson/unmarshaling_cases_test.go b/bson/unmarshaling_cases_test.go index 14049d63f7..dd38369bff 100644 --- a/bson/unmarshaling_cases_test.go +++ b/bson/unmarshaling_cases_test.go @@ -11,7 +11,6 @@ import ( "go.mongodb.org/mongo-driver/bson/bsonrw" "go.mongodb.org/mongo-driver/bson/bsontype" - "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" ) type unmarshalingTestCase struct { @@ -175,12 +174,6 @@ func unmarshalingTestCases() []unmarshalingTestCase { want: &valNonPtrStruct, data: docToBytes(valNonPtrStruct), }, - { - name: "null-literal data for custom type and unmarshaler should not be initialize", - sType: reflect.TypeOf(unmarshalerPtrStruct{}), - want: &unmarshalerPtrStruct{}, - data: docWithNullValueBytes("I"), - }, } } @@ -206,6 +199,25 @@ type unmarshalerNonPtrStruct struct { type myInt64 int64 +var _ ValueUnmarshaler = (*myInt64)(nil) + +func (mi *myInt64) UnmarshalBSONValue(t bsontype.Type, bytes []byte) error { + if len(bytes) == 0 { + return nil + } + + if t == bsontype.Int64 { + i, err := bsonrw.NewBSONValueReader(bsontype.Int64, bytes).ReadInt64() + if err != nil { + return err + } + + *mi = myInt64(i) + } + + return nil +} + func (mi *myInt64) UnmarshalBSON(bytes []byte) error { if len(bytes) == 0 { return nil @@ -257,14 +269,3 @@ func (ms *myString) UnmarshalBSON(bytes []byte) error { *ms = myString(s) return nil } - -// create a byte slice that represents BSON with a variable key value that is -// null, e.g. {: null}. -func docWithNullValueBytes(key string) []byte { - idx, doc := bsoncore.AppendDocumentStart(nil) - doc = bsoncore.AppendNullElement(doc, key) - - doc, _ = bsoncore.AppendDocumentEnd(doc, idx) - - return Raw(doc) -}