Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5222

Always mongoize uncastable values to nil

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.0.1
    • Affects Version/s: None
    • Component/s: Attributes
    • Labels:
      None

      mongoize is the method that is called during a field assignment to convert user-supplied value to the value to be stored in the attributes hash. In other words, mongoize performs type casting of attribute values during attribute setting.

      Depending on the types involved, when the provided value does not (sensibly) convert to the declared field type, mongoize behaves differently. It can:

      • set the attribute to nil instead of the provided value
      • set the attribute to some kind of a type-specific default value
      • raise an exception

      Example test:

      class Mop
        include Mongoid::Document
      
        field :int_field, type: Integer
        field :array_field, type: Array
        field :date_field, type: Date
        field :time_field, type: Time
        field :datetime_field, type: DateTime
        field :big_decimal_field, type: BigDecimal
        field :decimal128_field, type: BSON::Decimal128
        field :symbol_field, type: Symbol
        field :bson_symbol_field, type: BSON::Symbol::Raw
        field :regexp_field, type: Regexp
        field :bson_regexp_field, type: BSON::Regexp::Raw
      end
      
      irb(main):039:0> a=Mop.new(int_field:'x',regexp_field:'x',symbol_field:Time.now,datetime_field:'x',
      => #<Mop _id: 61dffd78a15d5d6a60e9946e, int_field: 0, array_field: nil, date_field: nil, time_...
      irb(main):040:0> a.int_field
      => 0
      irb(main):041:0> a.regexp_field
      => /x/
      irb(main):042:0> a.symbol_field
      => nil
      irb(main):043:0> a.datetime_field
      => nil
      irb(main):044:0> a.regexp_field
      => /x/
      
      
      irb(main):056:0> a=Mop.new(int_field:'x',regexp_field:42)
      Traceback (most recent call last):
              2: from (irb):55
              1: from (irb):55:in `new'
      TypeError (no implicit conversion of Integer into String)
      
      
      irb(main):059:0> a.regexp_field=false
      Traceback (most recent call last):
              1: from (irb):59
      TypeError (no implicit conversion of false into String)
      irb(main):060:0> a.time_field=false
      => false
      irb(main):061:0> a.time_field
      => nil
      
      
      

      Mongoid should be consistent in how un-typecastable input is assigned to fields. Once this is done, the behavior should be documented.

      As part of the scope written in WRITING-11159, we decided the following:

      • We will create a feature flag that will allow the user to raise on uncastable value.
      • If that feature flag is off, nil will always be returned for uncastable value.
      • Revised design *
      • Mongoization of uncastable values will always return nil
      • Original value will be accessible in attributes_before_type_cast (already existing behavior of Mongoid)

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: