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

Prohibit using undefined string/symbol types when declaring fields

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 8.0.1
    • Affects Version/s: None
    • Component/s: Attributes
    • Labels:
      None
    • Minor Change

      Split from https://jira.mongodb.org/browse/MONGOID-5210.

      Currently Mongoid accepts any string/symbol values as field types. However only values that are mapped by Mongoid to known field type classes actually work, the other values produce errors like the following:

      class A
        include Mongoid::Document
        
        field :foo, type: :bogus
      end
      
      A.new(foo: 42)
      
      
      ArgumentError: wrong number of arguments (given 1, expected 0)
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/extensions/object.rb:126:in `mongoize'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/3.1.0/forwardable.rb:238:in `mongoize'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/attributes.rb:256:in `typed_value_for'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/attributes.rb:157:in `block in write_attribute'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/threaded/lifecycle.rb:30:in `_assigning'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/attributes.rb:153:in `write_attribute'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/fields.rb:522:in `block (2 levels) in create_field_setter'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/attributes/processing.rb:91:in `process_attribute'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/attributes/processing.rb:24:in `block in process_attributes'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/attributes/processing.rb:22:in `each_pair'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/attributes/processing.rb:22:in `process_attributes'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/document.rb:110:in `block in initialize'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/threaded/lifecycle.rb:80:in `_building'
      /home/w/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/mongoid-7.4.0/lib/mongoid/document.rb:105:in `initialize'
      /home/w/apps/tests/ma/lib/tasks/test_5327.rake:9:in `new'
      /home/w/apps/tests/ma/lib/tasks/test_5327.rake:9:in `block in <top (required)>'
      

      The unknown string/symbol values should be detected during field definition and prohibited.

      The logic for defining fields is questionable in that the list of aliases is updated before the final type of the field is determined. Since the type determination may fail after this ticket is implemented, the alias definition will need to be moved after to avoid having the alias be added while the field is not due to bogus type.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: