Skip to content

GODRIVER-3436 Avoid initializing null data given custom decoder #1903

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

Merged
merged 1 commit into from
Dec 6, 2024

Conversation

prestonvasquez
Copy link
Member

GODRIVER-3436

Summary

If the value reader is null, prevent DefaultValueDecoders from initializing a pointer to the zero value.

Background & Motivation

The current behavior will lead to unexpected results:

ill instantiate a pointer field decoded from null data if the user defines a UnmarshalBSONValue. For example:

package main

import (
	"fmt"

	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

type DBInt64 int64

type Product struct {
	TotalForSell *DBInt64 `json:"total_for_sell" bson:"total_for_sell,omitempty"`
}

func (i *DBInt64) UnmarshalBSONValue(t bson.Type, value []byte) error {
	return nil
}

func main() {
	idx, doc := bsoncore.AppendDocumentStart(nil)
	doc = bsoncore.AppendNullElement(doc, "total_for_sell")

	doc, err := bsoncore.AppendDocumentEnd(doc, idx)
	if err != nil {
		panic(err)
	}

	bytes := bson.Raw(doc)

	got := Product{}
	if err := bson.Unmarshal(bytes, &got); err != nil {
		panic(err)
	}

	if got.TotalForSell != nil {
		fmt.Println("null value decoded as non-nil")
	}
}

Output:

❯ go run custom_type_with_pointer.go
null value decoded as non-nil

@prestonvasquez prestonvasquez added the priority-1-high High Priority PR for Review label Dec 5, 2024
Copy link
Contributor

API Change Report

./v2/mongo

incompatible changes

##Connect: changed from func(...*./v2/mongo/options.ClientOptions) (*Client, error) to func(..../v2/mongo/options.Lister[./v2/mongo/options.ClientOptions]) (*Client, error)

./v2/mongo/options

incompatible changes

(*AutoEncryptionOptions).SetBypassAutoEncryption: removed
(*AutoEncryptionOptions).SetBypassQueryAnalysis: removed
(*AutoEncryptionOptions).SetEncryptedFieldsMap: removed
(*AutoEncryptionOptions).SetExtraOptions: removed
(*AutoEncryptionOptions).SetKeyVaultClientOptions: removed
(*AutoEncryptionOptions).SetKeyVaultNamespace: removed
(*AutoEncryptionOptions).SetKmsProviders: removed
(*AutoEncryptionOptions).SetSchemaMap: removed
(*AutoEncryptionOptions).SetTLSConfig: removed
(*ClientOptions).ApplyURI: removed
(*ClientOptions).SetAppName: removed
(*ClientOptions).SetAuth: removed
(*ClientOptions).SetAutoEncryptionOptions: removed
(*ClientOptions).SetBSONOptions: removed
(*ClientOptions).SetCompressors: removed
(*ClientOptions).SetConnectTimeout: removed
(*ClientOptions).SetDialer: removed
(*ClientOptions).SetDirect: removed
(*ClientOptions).SetDisableOCSPEndpointCheck: removed
(*ClientOptions).SetDriverInfo: removed
(*ClientOptions).SetHTTPClient: removed
(*ClientOptions).SetHeartbeatInterval: removed
(*ClientOptions).SetHosts: removed
(*ClientOptions).SetLoadBalanced: removed
(*ClientOptions).SetLocalThreshold: removed
(*ClientOptions).SetLoggerOptions: removed
(*ClientOptions).SetMaxConnIdleTime: removed
(*ClientOptions).SetMaxConnecting: removed
(*ClientOptions).SetMaxPoolSize: removed
(*ClientOptions).SetMinPoolSize: removed
(*ClientOptions).SetMonitor: removed
(*ClientOptions).SetPoolMonitor: removed
(*ClientOptions).SetReadConcern: removed
(*ClientOptions).SetReadPreference: removed
(*ClientOptions).SetRegistry: removed
(*ClientOptions).SetReplicaSet: removed
(*ClientOptions).SetRetryReads: removed
(*ClientOptions).SetRetryWrites: removed
(*ClientOptions).SetSRVMaxHosts: removed
(*ClientOptions).SetSRVServiceName: removed
(*ClientOptions).SetServerAPIOptions: removed
(*ClientOptions).SetServerMonitor: removed
(*ClientOptions).SetServerMonitoringMode: removed
(*ClientOptions).SetServerSelectionTimeout: removed
(*ClientOptions).SetTLSConfig: removed
(*ClientOptions).SetTimeout: removed
(*ClientOptions).SetWriteConcern: removed
(*ClientOptions).SetZlibLevel: removed
(*ClientOptions).SetZstdLevel: removed
(*ClientOptions).Validate: removed
(*LoggerOptions).SetComponentLevel: removed
(*LoggerOptions).SetMaxDocumentLength: removed
(*LoggerOptions).SetSink: removed
(*ServerAPIOptions).SetDeprecationErrors: removed
(*ServerAPIOptions).SetStrict: removed
AutoEncryption: changed from func() *AutoEncryptionOptions to func() *AutoEncryptionOptionsBuilder
AutoEncryptionOptions.KeyVaultClientOptions: changed from *ClientOptions to Lister[ClientOptions]
Client: changed from func() *ClientOptions to func() *ClientOptionsBuilder
ClientOptions.AutoEncryptionOptions: changed from *AutoEncryptionOptions to Lister[AutoEncryptionOptions]
ClientOptions.LoggerOptions: changed from *LoggerOptions to Lister[LoggerOptions]
ClientOptions.ServerAPIOptions: changed from *ServerAPIOptions to Lister[ServerAPIOptions]
Logger: changed from func() *LoggerOptions to func() *LoggerOptionsBuilder
MergeClientOptions: removed
ServerAPI: changed from func(ServerAPIVersion) *ServerAPIOptions to func(ServerAPIVersion) *ServerAPIOptionsBuilder

compatible changes

AutoEncryptionOptionsBuilder: added
ClientOptionsBuilder: added
LoggerOptionsBuilder: added
ServerAPIOptionsBuilder: added

./v2/x/mongo/driver/topology

incompatible changes

##ConvertToDriverAPIOptions: changed from func(*./v2/mongo/options.ServerAPIOptions) *./v2/x/mongo/driver.ServerAPIOptions to func(./v2/mongo/options.Lister[./v2/mongo/options.ServerAPIOptions]) ./v2/x/mongo/driver.ServerAPIOptions
##NewConfig: changed from func(
./v2/mongo/options.ClientOptions, *./v2/x/mongo/driver/session.ClusterClock) (Config, error) to func(./v2/mongo/options.ClientOptionsBuilder, *./v2/x/mongo/driver/session.ClusterClock) (*Config, error)

compatible changes

NewConfigFromOptions: added

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 46119b4 into mongodb:master Dec 6, 2024
29 of 35 checks passed
@prestonvasquez prestonvasquez deleted the GODRIVER-3436-v2 branch December 6, 2024 17:46
@prestonvasquez prestonvasquez changed the title GODRIVER-3434 Avoid initializing null data given custom decoder GODRIVER-3436 Avoid initializing null data given custom decoder Jan 28, 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.

2 participants