Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-4887

serializeInto does not check for the presence of a toBSON method for values in Map entries

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • bson-5.0.0
    • Affects Version/s: None
    • Component/s: BSON
    • 2
    • 2
    • Not Needed
    • Not Needed

      Our serializeInto function currently checks if any passed in values to be serialized
      have a toBSON method defined on them in all cases excepting the values of
      Map entries.

      The omission of this check seems to be in error and may produce unexpected results
      for users who expect consistent behaviour across supplied values.

      User Impact

      Unsure as we do not know how many users rely on this feature being consistent for their workflows.

      Acceptance Criteria

      • serializeInto checks for toBSON method on values of Map entries
      • Write tests to ensure that we check for toBSON method on values passed to serializedInto on all paths

      Steps to reproduce

      The following code snippet demonstrates the difference the serialization and subsequent
      deserialization of an Object and a Map with the same keys and values.

      const BSON = require('bson');
      
      // Add toBSON method to numbers
      Number.prototype.toBSON = function() {
        return `Number: ${this.valueOf()}`
      }
      
      const object = {"a": 1, "b":2};
      const map = new Map();
      map.set("a", 1);
      map.set("b", 2);
      
      console.log(BSON.deserialize(BSON.serialize(map)));
      // { a: 1, b: 2 }
      console.log(BSON.deserialize(BSON.serialize(object)));
      // { a: 'Number: 1', b: 'Number: 2' }
      

            Assignee:
            warren.james@mongodb.com Warren James
            Reporter:
            warren.james@mongodb.com Warren James
            Bailey Pearson
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: