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

Fix "syntax error" with dynamic fields

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.15
    • Affects Version/s: None
    • Component/s: None
    • None

      While running under JRuby I noticed the following error while setting dynamic fields via []:

      SyntaxError: (eval):1: syntax error, unexpected tMINUS
      
              def sci-fi=(value)
                     ^
              org/jruby/RubyModule.java:2286:in `module_eval'
              /usr/local/lib/jruby/gems/gems/activesupport-3.2.8/lib/active_support/core_ext/kernel/singleton_class.rb:11:in `class_eval'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes.rb:214:in `define_dynamic_writer'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes.rb:228:in `method_missing'
              org/jruby/RubyBasicObject.java:1704:in `__send__'
              org/jruby/RubyKernel.java:2130:in `send'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes/processing.rb:124:in `process_attribute'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes/processing.rb:28:in `process_attributes'
              org/jruby/RubyHash.java:1211:in `each_pair'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes/processing.rb:26:in `process_attributes'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes/processing.rb:190:in `with_mass_assignment'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes/processing.rb:187:in `with_mass_assignment'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes/processing.rb:22:in `process_attributes'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes.rb:158:in `assign_attributes'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/threaded/lifecycle.rb:26:in `_assigning'
              /usr/local/lib/jruby/gems/gems/mongoid-3.0.10/lib/mongoid/attributes.rb:157:in `assign_attributes'
      

      The root cause seems to be the call to define_dynamic_writer without verifying that the method name is a valid Ruby identifier. This then calls class_eval with invalid syntax. I put a guard in process_attribute as well as the define_dynamic_* methods as a defense in depth against future errors. The new spec test was added and failing before this change.

      Somewhat related, is there any worry about these class_eval calls causing bloat over time when using high numbers of dynamic attributes?

            Assignee:
            Unassigned Unassigned
            Reporter:
            mzsanford Matt Sanford
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: