Detailed steps to reproduce the problem?
This unit test passes on v1.17.1 It looks like in v1.17.2 a change was mode so
UnmarshalBSONValue() is not called if the value is type bson.TypeNull
This breaks my application because I depend on initializing a field in UnmarshalBSONValue if the value in the bson document is null.
I suspect its related to this PR but im not sure:
https://github.com/mongodb/mongo-go-driver/pull/1903/commits/6cf87873eece1ff399686b41f3260ca7a3bb4d15
Here is a one file go unit test that reproduces this issue. If you this unit test on v1.17.2 it fails. If you run it on v1.17.1 it passes and keeps wit the behavior of the driver for several years at least.
package unmarshaltest import ( "errors" "fmt" "github.com/RoaringBitmap/roaring" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" "testing" ) type MockStruct struct { ID string `bson:"_id"` Overrider MockOverrider `bson:"overrider"` } type MockOverrider struct { bitmap *roaring.Bitmap } func (m MockOverrider) MarshalBSONValue() (bsontype.Type, []byte, error) { if m.bitmap == nil || m.bitmap.IsEmpty() { return bson.TypeNull, nil, nil } bytes, err := m.bitmap.MarshalBinary() if err != nil { return bson.TypeUndefined, nil, err } return bson.MarshalValue(bytes) } func (m *MockOverrider) UnmarshalBSONValue(t bsontype.Type, raw []byte) error { m.bitmap = roaring.New() if t == bson.TypeNull { return nil } if t != bson.TypeBinary { return fmt.Errorf("unable to unmarshal Bitmap from bson type: %v", t) } _, bytes, _, ok := bsoncore.ReadBinary(raw) if !ok { return errors.New("unable to ReadBinary to unmarshal Bitmap") } err := m.bitmap.UnmarshalBinary(bytes) if err != nil { return fmt.Errorf("unable to unmarshal bson byte array to unmarshal Bitmap: %w", err) } return nil } func TestUnmarshalNilBehavior(t *testing.T) { // marshal a mock struct with a nil bitmap expected := MockStruct{ ID: "test", } expectedBytes, err := bson.Marshal(&expected) require.Nil(t, err) // unmarshal var found MockStruct err = bson.Unmarshal(expectedBytes, &found) require.Nil(t, err) // bitmap should not be nil assert.NotNil(t, found.Overrider.bitmap) }
Definition of done: what must be done to consider the task complete?
Unit test given passes
The exact Go version used, with patch level:
$ go version
go version go1.23.5 darwin/arm64
The exact version of the Go driver used:
$ go list -m go.mongodb.org/mongo-driver
Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.
N/A
The operating system and version (e.g. Windows 7, OSX 10.8, ...) N/A
Security Vulnerabilities
If you’ve identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here
- related to
-
GODRIVER-3436 DefaultValueDecoders.ValueUnmarshalerDecodeValue attempts to unmarshal bsontype.Null
- Closed