Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-917

Difference in BSON/JSON serialization of nil primitive.D compared to mgo

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.3.0
    • Component/s: BSON
    • Labels:

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              isabella.siu Isabella Siu (Inactive)
              Reporter:
              john.morales John Morales
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: