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

Primitive UnmarshalJSON method should return a zero'd objectId when the field is empty instead of a decode error

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 1.4.0
    • Affects Version/s: 1.0.1
    • Component/s: JSON & ExtJSON
    • Labels:
      None

      Currently, when ObjectIds are unmarshalled from a JSON payload https://github.com/mongodb/mongo-go-driver/blob/master/bson/primitive/objectid.go#L100 , 64 and 12 byte strings are properly converted to an objectId. All others return decoding errors. This is expected functionality.

       

      However an empty string or empty field also returns an error instead of a zero/null valued objectId. This makes using the primitive.ObjectID type within a struct definition very opinionated, and negates the need for IsZero methods. For example, if a service layer is responsible for decoding a struct and then setting ObjectID values based on claims information, the request will hit a decoding error since the payload will not contain the objectIds.

      It forces the use of struct composition to decode input values, by necessitating the extension to a more complete struct definition to apply the ObjectIds. It could very easily be argued that this is a best practice, but it should not be the responsibility of the mongo-driver to force such a high level design decision

       

      Request: ObjectIds should be decoded for 64/12 bytes and for empty fields it should initialize a zero valued objectId that can then be validated, initialized or handled in the application logic

            Assignee:
            divjot.arora@mongodb.com Divjot Arora (Inactive)
            Reporter:
            qhenkart@gmail.com Quest Henkart
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: