Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-2954

Decimal128 should deserialize to BigDecimal

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      (* I am raising this ticket for driver, but the solution may also partly be in Mongoid)

      While testing for MONGOID-4500, I observed the following behavior:
       

      class Band
         ...
         field :mojo, type: Object
      end
      
      band = Band.create!(mojo: BigDecimal('123'))
      
      band.mojo #=> returns BSON::Decimal128 object !!

      This is problematic to get a BSON::Decimal128, because: * BSON::Decimal128#numeric? returns false

      • BSON::Decimal128 does not implement Ruby stdlib Numeric interface, i.e. methods like #to_i, #to_f, #coerce etc.

      I think Mongo team should decide whether to:

      • A) Make BSON::Decimal128 should be a proper Numeric-like object, i.e. implement #to_i, #to_f, #coerce and .is_a?(Numeric), numeric? etc. These could delegate to to_big_decimal for now. This should then be done for all BSON numeric types, e.g. BSON::Int32, BSON::Int64, etc.
      • B) Always convert BSON::Decimal128 to BigDecimal ideally at the driver level--never expose it to the user in a way that it could accidentally get mixed up in numeric calculations. Note that this is the current approach I believe with Integer, Float, etc.
      • C) Do both A and B – have (B) "always convert to BigDecimal in driver" be the golden path, but implement (A) for safety to avoid app errors.

      Approach B or C would be my preference.

            Assignee:
            Unassigned Unassigned
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: