[GODRIVER-917] Difference in BSON/JSON serialization of nil primitive.D compared to mgo Created: 01/Apr/19  Updated: 28/Oct/23  Resolved: 15/Jan/20

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 1.3.0

Type: New Feature Priority: Major - P3
Reporter: John Morales Assignee: Isabella Siu (Inactive)
Resolution: Fixed Votes: 0
Labels: mgocompat
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-1194 Confusing error using uninitialized b... Closed
is related to GODRIVER-1216 mgo compatible bson registry Closed

 Description   

Consider the following demonstrative test case example comparing nil vs empty BSON serialization:

package components
 
import (
    "testing"
 
    mgobson "gopkg.in/mgo.v2/bson"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
)
 
func TestDriverSerialization_PrimativeD_GoDriver(test *testing.T) {
    var data primitive.D
    buf, err := bson.Marshal(data)
    if err != nil {
        test.Fatalf("Failed to marshal primitive.D %+v, Err: %v", data, err)
    }
 
    test.Logf("Bytes: %v", buf)
    if len(buf) != 5 {
        test.Errorf("Expected marshaled *NamespaceInfo to be 5 bytes, was: %v", len(buf))
    }
}
 
func TestDriverSerialization_BsonD_Mgo(test *testing.T) {
    var data mgobson.D
    buf, err := mgobson.Marshal(data)
    if err != nil {
        test.Fatalf("Failed to marshal primitive.D %+v, Err: %v", data, err)
    }
 
    test.Logf("Bytes: %v", buf)
    if len(buf) != 5 {
        test.Errorf("Expected marshaled *NamespaceInfo to be 5 bytes, was: %v", len(buf))
    }
}

Output (mac os 10.13, mongo-do-driver 1.0.0):

$ go test -v -run TestDriverSerialization
=== RUN   TestDriverSerialization_PrimativeD_GoDriver
--- FAIL: TestDriverSerialization_PrimativeD_GoDriver (0.00s)
    mothership_serialization_test.go:15: Failed to marshal primitive.D [], Err: WriteNull can only write while positioned on a Element or Value but is positioned on a TopLevel
=== RUN   TestDriverSerialization_BsonD_Mgo
--- PASS: TestDriverSerialization_BsonD_Mgo (0.00s)
    mothership_serialization_test.go:31: Bytes: [5 0 0 0 0]

  • In case of mgo, an uninitialized bson.D serializes to an empty document ([5, 0, 0, 0, 0]).
  • In case of mongo-go-driver, an uninitialized primitive.D results in an error.
  • In case of mongo-go-driver, an empty data := make(primitive.D, 0) results in an empty document.

It's not clear to me what's the expected behavior, hence opening this ticket as type Question. However it did come up as a behavior change when converting an existing project from mgo to mongo-go-driver.



 Comments   
Comment by Isabella Siu (Inactive) [ 15/Jan/20 ]

An option to match mgo's behavior was added as part of mgocompat and can be used through the mgocompat.MgoRegistry registry.

Comment by Ian Whalen (Inactive) [ 04/Apr/19 ]

in order to avoid making any backwards breaking changes, we think it might make sense to make this an encoding context option.

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