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

Disallow using bson.E as value type in a BSON document.

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • None

      The Go BSON library currently allows using a bson.E as the value in a BSON document defined using the bson.D BSON document literal syntax. However, when that happens, the user almost certainly intended to define a nested BSON document with a single field, not define a nested document with the structure of a bson.E.

      Consider the following bson.D document:

      bson.D{
      	{"serverStatus", 1},
      	{"metrics", bson.E{"apiVersions", 0}},
      }
      

      That value successfully marshals to a BSON document with a structure like:

      {
          "serverStatus": {"$numberInt": "1"},
          "metrics": {
              "key": "apiVersions",
              "value": {"$numberInt": "0"}
          }
      }
      

      Notice that the "metrics" field is a nested document with the structure

      { "key": "...", "value": ... }

      instead of

      { "apiVersions": ... }

      .

      It's extremely likely that what a user meant to do was define a document where "metrics" is a nested document, like:

      bson.D{
      	{"serverStatus", 1},
      	{"metrics", bson.D{{"apiVersions", 0}}},
      }
      

      Which marshals to a BSON document with the more expected structure:

      {
          "serverStatus": {"$numberInt": "1"},
          "metrics": {
              "apiVersions": {"$numberInt": "0"}
          }
      }
      

      That bug is very difficult to track down because the Go BSON document literal definitions look so similar. It's also not immediately obvious that the resulting BSON is wrong because the Key: "key", Value: <value> pattern appears frequently when inspecting Go BSON document literals. We should return an error if a user attempts to use a bson.E as a value in a bson.E because it's almost certainly not what they intended to do.

      Open questions:

      • Should we disallow using a bson.E as a value in a bson.M? How about in a user-defined struct?
      • Should we make bson.E marshal to a single-field document when it's used as a bson.D.Value, instead of returning an error?

      Definition of done:

      • Return an error if a user attempts to use a bson.E as the value type in a bson.E.

            Assignee:
            Unassigned Unassigned
            Reporter:
            matt.dale@mongodb.com Matt Dale
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: