Skip to content
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

GODRIVER-3470 Correct BSON unmarshaling logic for null values #1924

Merged
merged 6 commits into from
Feb 19, 2025

Conversation

prestonvasquez
Copy link
Collaborator

GODRIVER-3470

Summary

Ensure UnmarshalBSONValue is bypassed and the Go pointer is set to nil ONLY when the Go type is a pointer and the BSON value is null.

Background & Motivation

PR #1903 introduced logic where UnmarshalBSONValue() is not called for bson.TypeNull, which breaks applications that rely on this behavior for initializing fields.

@prestonvasquez prestonvasquez added the priority-2-medium Medium Priority PR for Review label Jan 29, 2025
Copy link
Contributor

API Change Report

No changes found!

@prestonvasquez prestonvasquez added priority-1-high High Priority PR for Review and removed priority-2-medium Medium Priority PR for Review labels Feb 3, 2025
PtrTracker *unmarshalCallTracker `bson:"ptr_tracker"`
}

func (ms *unmarshalCallTracker) UnmarshalBSONValue(bsontype.Type, []byte) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also test this behavior for UnmarshalBSON?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

qingyang-hu
qingyang-hu previously approved these changes Feb 3, 2025
Copy link
Collaborator

@qingyang-hu qingyang-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

matthewdale
matthewdale previously approved these changes Feb 6, 2025
Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looks good! 👍

Open question about using the same set of conditions in ValueUnmarshalerDecodeValue and UnmarshalerDecodeValue so it's easier to understand in the future.

// directly set to nil here. Since the pointer is being replaced with nil,
// there is no opportunity (or reason) for the custom UnmarshalBSONValue logic
// to be called.
if vr.Type() == bsontype.Null && val.Kind() == reflect.Ptr {
Copy link
Collaborator

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 UnmarshalerDecodeValue after the value is read into a []byte:

if val.Kind() == reflect.Ptr && len(src) == 0 {
	val.Set(reflect.Zero(val.Type()))
	return nil
}

Can we use the same condition in both methods? Or are they distinct scenarios?

Copy link
Collaborator Author

@prestonvasquez prestonvasquez Feb 6, 2025

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/files

Since the bytes represent BSON null are copied, the type is converted from null to invalid:

	_, src, err := bsonrw.Copier{}.CopyValueToBytes(vr)
	if err != nil {
		return err
	}

Which means that this check doesn’t work:

	if val.Kind() == reflect.Ptr && vr.Type() == bsontype.Null {
		val.Set(reflect.Zero(val.Type()))
		return nil
	}

Checking after copying is weaker since validity should be checked on the first block:

	if !val.IsValid() || (!val.Type().Implements(tValueUnmarshaler) && !reflect.PtrTo(val.Type()).Implements(tValueUnmarshaler)) {
		return ValueDecoderError{Name: "ValueUnmarshalerDecodeValue", Types: []reflect.Type{tValueUnmarshaler}, Received: val}
	}

So I think BSON null is the only case where you would get an invalid type after copying. I suggest we mirror ValueUnmarshalerDecodeValue.

qingyang-hu
qingyang-hu previously approved these changes Feb 7, 2025
Comment on lines 1585 to 1588
if val.Kind() == reflect.Ptr && len(src) == 0 {
val.Set(reflect.Zero(val.Type()))
return nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this will result in UnmarshalBSON being called with an empty byte slice for BSON minkey and maxkey value. Because the byte slice passed to UnmarshalBSON has the BSON type byte stripped off it, zero-length BSON values will be indistinguishable from each other (null, minkey, maxkey). Because of that limitation, we should keep the existing behavior.

Here are two test cases that can be added to unmarshalingTestCases that demonstrate the behavior:

{
	name:  "nil pointer and non-pointer type with BSON minkey",
	sType: reflect.TypeOf(unmarshalBehaviorTestCase{}),
	want: &unmarshalBehaviorTestCase{
		BSONValueTracker: unmarshalBSONValueCallTracker{
			called: true,
		},
		BSONValuePtrTracker: &unmarshalBSONValueCallTracker{
			called: true,
		},
		BSONTracker: unmarshalBSONCallTracker{
			called: true,
		},
		BSONPtrTracker: nil,
	},
	data: docToBytes(D{
		{Key: "bv_tracker", Value: primitive.MinKey{}},
		{Key: "bv_ptr_tracker", Value: primitive.MinKey{}},
		{Key: "b_tracker", Value: primitive.MinKey{}},
		{Key: "b_ptr_tracker", Value: primitive.MinKey{}},
	}),
},
{
	name:  "nil pointer and non-pointer type with BSON maxkey",
	sType: reflect.TypeOf(unmarshalBehaviorTestCase{}),
	want: &unmarshalBehaviorTestCase{
		BSONValueTracker: unmarshalBSONValueCallTracker{
			called: true,
		},
		BSONValuePtrTracker: &unmarshalBSONValueCallTracker{
			called: true,
		},
		BSONTracker: unmarshalBSONCallTracker{
			called: true,
		},
		BSONPtrTracker: nil,
	},
	data: docToBytes(D{
		{Key: "bv_tracker", Value: primitive.MaxKey{}},
		{Key: "bv_ptr_tracker", Value: primitive.MaxKey{}},
		{Key: "b_tracker", Value: primitive.MaxKey{}},
		{Key: "b_ptr_tracker", Value: primitive.MaxKey{}},
	}),
},

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, updated!

Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👍

@prestonvasquez prestonvasquez merged commit 659342c into mongodb:v1 Feb 19, 2025
31 of 34 checks passed
@prestonvasquez prestonvasquez deleted the GODRIVER-3470 branch February 19, 2025 02:29
matthewdale added a commit to matthewdale/mongo-go-driver that referenced this pull request Feb 19, 2025
matthewdale added a commit that referenced this pull request Feb 19, 2025
@matthewdale
Copy link
Collaborator

drivers-pr-bot please backport to release/1.17

mongodb-drivers-pr-bot bot pushed a commit that referenced this pull request Feb 19, 2025
… [master] (#1945)

Co-authored-by: Preston Vasquez <[email protected]>
(cherry picked from commit 25df82f)
Copy link
Contributor

Sorry, unable to cherry-pick to release/1.17, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout -b cherry-pick-release/1.17-659342c5d8d8436dcfe7863026808d14505c225d release/1.17

git fetch origin 659342c5d8d8436dcfe7863026808d14505c225d
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 659342c5d8d8436dcfe7863026808d14505c225d
  1. You will likely have some merge/cherry-pick conflicts here, fix them and commit:
git commit -am {message}
  1. Push to a named branch:
git push origin cherry-pick-release/1.17-659342c5d8d8436dcfe7863026808d14505c225d
  1. Create a PR against branch release/1.17. I would have named this PR:

"GODRIVER-3470 Correct BSON unmarshaling logic for null values (#1924) [release/1.17]"

matthewdale added a commit that referenced this pull request Feb 19, 2025
matthewdale pushed a commit to matthewdale/mongo-go-driver that referenced this pull request Feb 20, 2025
matthewdale added a commit that referenced this pull request Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-1-high High Priority PR for Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants