js-bson cannot round-trip documents that contain _bsontype as a plain field name

XMLWordPrintableJSON

    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Use Case

      As a MongoDB Node.js driver user
      I want documents containing `_bsontype` as a plain data field to serialize correctly
      So that documents inserted by other drivers can be read and written back without errors

      User Experience

      • A document like ` { _bsontype: "foo", value: 1 }

        ` stored by any driver deserializes to a plain JS object fine, but cannot be re-serialized. At root level it throws `BSONError: BSON types cannot be serialized as a document`; nested, it throws `BSONVersionError` because the plain deserialized object has no `@@mdb.bson.version` symbol. Read-modify-write on such documents is broken.

      Dependencies

      • The fix involves updating the root-level `_bsontype` guard (added in #537) and the nested version-symbol gate (added in #709) to use the `bsonType` symbol (`@@mdb.bson.type`) for dispatch instead of the `_bsontype` string property.
      • `calculate_size.ts` dispatches on `_bsontype` as well and needs the same change.

      Risks/Unknowns

      • Lifting the root-level guard (a deliberate BSON v5 breaking change, #537) needs careful review to confirm real BSON type instances at the root still throw.
      • Changing the nested gate from `value._bsontype == null` to `value[bsonType] == null` means objects with `_bsontype` as a plain string but no `bsonType` symbol will serialize as plain documents instead of throwing `BSONVersionError`. That is the desired fix, but it is a behavior change.

      Acceptance Criteria

      Implementation Requirements

      • `bson.deserialize(bson.serialize(doc))` round-trips cleanly for documents with `_bsontype` as a plain string field, at root and nested.
      • Real BSON type instances (ObjectId, Int32, etc.) at the root still throw.
      • `calculateObjectSize` agrees with `serialize` for all affected cases.

      Testing Requirements

      • Round-trip ` { _bsontype: "anything" }

        ` at root and nested, confirm no throw.

      • Confirm BSON type instances at root still throw.
      • Existing serializer input-validation tests pass.

      Documentation Requirements

      • This is a breaking change - flag it in the release notes.

      Follow Up Requirements

      • N/A

            Assignee:
            Unassigned
            Reporter:
            Sergey Zelenov
            None
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: