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

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

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.3.0
    • Affects Version/s: None
    • Component/s: BSON

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

      Unable to find source-code formatter for language: golang. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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.

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

              Created:
              Updated:
              Resolved: