Use .fromString helpers in Double and Int32 Constructors and move Long.fromStringStrict functionality to Long.fromString

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • Not Needed
    • None
    • 0.2
    • 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 BSON user
      I want numeric constructors to throw on malformed input
      So that the value I input is the value that is stored

      Problem

      The Int32, Double and Long constructors silently coerce invalid strings: new Int32("abc") gives 0 (via +value | 0), new Double("abc") gives NaN (via +value), new Long("abc") gives 0.

      Long.fromString is worse, because it parses the input in 8-digit chunks, so the result depends on where the invalid characters land:

      Long.fromString("1234xxx5")    // 1234
      Long.fromString("1234xxxx5")   // 12345
      Long.fromString("1234xxxxx5")  // 123400
      

      Reported externally in NODE-3660 (2021) and NODE-5638 (2023, closed as a duplicate of this ticket).

      No deprecation is needed. The opt-in strict replacements already ship: Int32.fromString and Double.fromString in bson 6.6.0, Long.fromStringStrict in 6.7.0. This ticket makes them the default.

      Acceptance Criteria

      Implementation Requirements

      • The Int32 and Double constructors delegate to their fromString helpers
      • Move Long.fromStringStrict behavior into Long.fromString, which is also reached through the Long string constructor overload and Long.fromValue
      • Validation must cover radix 2 through 36. Long.fromExtendedJSON has a decimal-only regex that cannot be reused
      • Align the Double constructor signature, currently typed value: number but accepting strings at runtime, while Int32 is typed number | string

      Testing Requirements

      • Update constructor tests for the new error cases
      • Benchmark Long.fromString. The 8-digit chunking exists to avoid expensive emulated multiplication, so adding validation to a hot path needs measuring

      Documentation Requirements

      • Update the API docs for each constructor

            Assignee:
            Unassigned
            Reporter:
            Aditi Khare (Inactive)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: