[GODRIVER-585] omitempty behavior on serializing objectid.ObjectID value type in structs Created: 29/Sep/18  Updated: 28/Oct/23  Resolved: 08/Oct/18

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 0.0.14
Fix Version/s: 0.0.15

Type: Task Priority: Major - P3
Reporter: Mohammad Nasirifar Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

linux
go1.11


Attachments: File omitempty_test.go    

 Description   

omitempty behavior differs from how value types are expected to work. Especially objectid.NilObjectID can be quite misleading in such scenarios. Kindly go test the following or use the attached file.

package main
 
import (
	"testing"
 
	"github.com/mongodb/mongo-go-driver/bson"
	"github.com/mongodb/mongo-go-driver/bson/objectid"
)
 
type ThingOmitID struct {
	ID    objectid.ObjectID `bson:"_id,omitempty"`
	Field string            `bson:"field"`
}
 
type ThingOmitIDPtr struct {
	ID    *objectid.ObjectID `bson:"_id,omitempty"`
	Field string             `bson:"field"`
}
 
type ThingOmitStringField struct {
	ID    objectid.ObjectID `bson:"_id"`
	Field string            `bson:"field,omitempty"`
}
 
func TestOmitEmptyID_NilObjectID(t *testing.T) {
	thing := &ThingOmitID{
		ID:    objectid.NilObjectID,
		Field: "content",
	}
	validateNumKeys(t, 1, thing)
}
 
func TestOmitEmptyID_Omit(t *testing.T) {
	thing := &ThingOmitID{
		Field: "content",
	}
	validateNumKeys(t, 1, thing)
}
 
func TestOmitEmptyIDPtr_Nil(t *testing.T) {
	thing := &ThingOmitIDPtr{
		ID:    nil,
		Field: "content",
	}
	validateNumKeys(t, 1, thing)
}
 
func TestOmitEmptyIDPtr_NilObjectID(t *testing.T) {
	thing := &ThingOmitIDPtr{
		ID:    &objectid.NilObjectID,
		Field: "content",
	}
	validateNumKeys(t, 2, thing)
}
 
func TestOmitEmptyStringField_Empty(t *testing.T) {
	thing := &ThingOmitStringField{
		ID:    objectid.NilObjectID,
		Field: "",
	}
	validateNumKeys(t, 1, thing)
}
 
func TestOmitEmptyStringField_Omit(t *testing.T) {
	thing := &ThingOmitStringField{
		ID: objectid.NilObjectID,
	}
	validateNumKeys(t, 1, thing)
}
 
func validateNumKeys(t *testing.T, numKeys int, obj interface{}) {
	encodedObj, err := bson.NewDocumentEncoder().EncodeDocument(obj)
	if err != nil {
		t.Error(err.Error())
	}
 
	keys, err := encodedObj.Keys(false)
	if err != nil {
		t.Error(err.Error())
	}
	if numKeys != len(keys) {
		for _, key := range keys {
			t.Log(key.Prefix, key.Name)
		}
		t.Errorf("Expected number of keys: %d, actual: %d", numKeys, len(keys))
	}
}



 Comments   
Comment by Mohammad Nasirifar [ 02/Oct/18 ]

Looks like it's "fixed" by https://github.com/mongodb/mongo-go-driver/commit/71de844febff6ac5b6fc6ba165e24e9677d2ee05 if you manage to get your head around 10k lines of breaking change that was introduced as the side product.

Comment by Mohammad Nasirifar [ 29/Sep/18 ]

If this is not expected I would be able to submit a patch.

Generated at Thu Feb 08 08:34:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.