Make the Binary sub_type a number eagerly

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Fixed
    • Priority: Minor - P4
    • bson-7.2.1
    • Affects Version/s: None
    • Component/s: BSON
    • 2
    • 1
    • Not Needed
    • None
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      "The Binary constructor now normalizes subtype inputs to match BSON serialization. Previously, passing a stringified subtype like '4' would cause a mismatch between the sub_type property (string '4') and the serialized BSON value (0x04). The constructor now converts inputs to ensure consistency."

      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      Yes

      3. Which versions of the driver/connector does this apply to?
      js-bson 7.2.1 (current version on May 7th is 7.2.0)

      Show
      1. What would you like to communicate to the user about this feature? "The Binary constructor now normalizes subtype inputs to match BSON serialization. Previously, passing a stringified subtype like '4' would cause a mismatch between the sub_type property (string '4') and the serialized BSON value (0x04). The constructor now converts inputs to ensure consistency." 2. Would you like the user to see examples of the syntax and/or executable code and its output? Yes 3. Which versions of the driver/connector does this apply to? js-bson 7.2.1 (current version on May 7th is 7.2.0)
    • None
    • None
    • None
    • None
    • None
    • None

      Use Case

      As a... BSON user / shell user
      I want... Binary types to be created in the form they will be serialized in
      So that... inspecting them and performing transformations on them do not work with unexpected values

      User Experience

      • It is desirable that the in memory representation of the Binary object match how it will be serialized as early as possible.
      • Match how the Int32 class makes the number truncate to an int32 in the ctor like how it will eventually be truncated in the serializer.
      • It sort of leads to bugs:
      var uuid = new UUID();
      var b = Binary.createFromBase64(u.toString('base64'), '4'); // stringified sub_type!
      // ...
      b.toUUID();
      BSONError: Binary sub_type "4" is not supported for converting to UUID. Only "4" is currently supported. // hard to tell what this is pointing out
      // ...
      BSON.deserialize(BSON.serialize({ b })) // round trips perfectly fine
      { b: new UUID('1e54853f-d91f-41c7-9f98-f4e6faeec2da') }
      

      Dependencies

      • None

      Risks/Unknowns

      • None

      Acceptance Criteria

      Implementation Requirements

      • In the Binary constructor, match the serializer behavior and cast the subType input to a Number
      • Improve the toUUID() error message to make the type information of the subType clear

      Testing Requirements

      • Regression test for the number casting behavior
      • Check for the new improved error message

      Documentation Requirements

      • TBD? The typescript already claims the property to be a number, the fact that it can be something else seems like we're fixing reality to match the existing docs?

      Follow Up Requirements

      • Are there other error messages where we stringify the input losing information in the process? (like how the string "4" became indistinguishable from the number "4")

            Assignee:
            Sergey Zelenov
            Reporter:
            Neal Beeken
            Pavel Safronov
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: