ObjectId.toHexString() returns non-lowercase under cacheHexString when constructed from an uppercase hex string

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • 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

      When ObjectId.cacheHexString is enabled and an ObjectId is constructed from an uppercase or mixed-case 24-character hex string, the constructor caches the raw input verbatim. toHexString() then returns it as-is, violating its documented contract ("24 lowercase character hex string") and breaking equals(), whose string and ObjectId-like paths lowercase the other value and compare it against this.toHexString().

      Repro:

      ObjectId.cacheHexString = true;
      const oid = new ObjectId('AAAAAAAAAAAAAAAAAAAAAAAA');
      oid.toHexString();                       // 'AAAA...'  (expected lowercase)
      oid.equals('aaaaaaaaaaaaaaaaaaaaaaaa');  // false      (expected true)
      

      Fix: cache workingId.toLowerCase() in the string-constructor branch of src/objectid.ts (the id setter already normalizes via ByteUtils.toHex). Surfaced by Copilot review on PR #893.

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

              Created:
              Updated: