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

Open
wants to merge 3 commits into
base: v1
Choose a base branch
from

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!

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!

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?

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