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

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 1.0.1
    • Fix Version/s: 1.4.0
    • Component/s: JSON & ExtJSON
    • Security Level: Public
    • Labels:
      None

      Description

      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

        Attachments

          Activity

            People

            Assignee:
            divjot.arora Divjot Arora
            Reporter:
            qhenkart Quest Henkart
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: