-
Couldn't load subscription status.
- Fork 918
GODRIVER-3470 Correct BSON unmarshaling logic for null values #1924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
dc02142
f1e8a1d
7d2be3c
32190fe
75ee151
cd82ab3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,12 @@ package bson | |
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "go.mongodb.org/mongo-driver/bson/bsonrw" | ||
| "go.mongodb.org/mongo-driver/bson/bsontype" | ||
| "go.mongodb.org/mongo-driver/internal/assert" | ||
| "go.mongodb.org/mongo-driver/internal/require" | ||
| ) | ||
|
|
||
| type unmarshalingTestCase struct { | ||
|
|
@@ -114,6 +117,17 @@ func unmarshalingTestCases() []unmarshalingTestCase { | |
| }, | ||
| data: docToBytes(D{{"fooBar", int32(10)}}), | ||
| }, | ||
| { | ||
| name: "nil pointer and non-pointer type with literal null BSON", | ||
| sType: reflect.TypeOf(unmarshalBehaviorTestCase{}), | ||
| want: &unmarshalBehaviorTestCase{ | ||
| Tracker: unmarshalCallTracker{ | ||
| unmarshalCalled: true, | ||
| }, | ||
| PtrTracker: nil, | ||
| }, | ||
| data: docToBytes(D{{Key: "tracker", Value: nil}, {Key: "ptr_tracker", Value: nil}}), | ||
| }, | ||
| // GODRIVER-2252 | ||
| // Test that a struct of pointer types with UnmarshalBSON functions defined marshal and | ||
| // unmarshal to the same Go values when the pointer values are "nil". | ||
|
|
@@ -269,3 +283,35 @@ func (ms *myString) UnmarshalBSON(bytes []byte) error { | |
| *ms = myString(s) | ||
| return nil | ||
| } | ||
|
|
||
| type unmarshalCallTracker struct { | ||
| unmarshalCalled bool | ||
| } | ||
|
|
||
| type unmarshalBehaviorTestCase struct { | ||
| Tracker unmarshalCallTracker `bson:"tracker"` | ||
| PtrTracker *unmarshalCallTracker `bson:"ptr_tracker"` | ||
| } | ||
|
|
||
| func (ms *unmarshalCallTracker) UnmarshalBSONValue(bsontype.Type, []byte) error { | ||
|
||
| ms.unmarshalCalled = true | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func TestInitializedPointerDataWithBSONNull(t *testing.T) { | ||
| // Set up the test case with an initialized pointer. | ||
| tc := unmarshalBehaviorTestCase{ | ||
| PtrTracker: &unmarshalCallTracker{}, | ||
| } | ||
|
|
||
| // Create BSON data where the 'ptr_tracker' field is explicitly set to null. | ||
| bytes := docToBytes(D{{Key: "ptr_tracker", Value: nil}}) | ||
|
|
||
| // Unmarshal the BSON data into the test case struct. | ||
| // This should set PtrTracker to nil due to the BSON null value. | ||
| err := Unmarshal(bytes, &tc) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Nil(t, tc.PtrTracker) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a similar block in
UnmarshalerDecodeValueafter the value is read into a[]byte:Can we use the same condition in both methods? Or are they distinct scenarios?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
len(src)check was implemented here: https://github.com/mongodb/mongo-go-driver/pull/833/filesSince the bytes represent BSON null are copied, the type is converted from null to invalid:
Which means that this check doesn’t work:
Checking after copying is weaker since validity should be checked on the first block:
So I think BSON null is the only case where you would get an invalid type after copying. I suggest we mirror
ValueUnmarshalerDecodeValue.