9.0.8 fix for MONGOID-5888 not complete - validation bypassed in creations

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Ruby Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Related issue: MONGOID-5888

      Recap: In 9.0.7, a change was made that broken validation of deeply nested children

      In 9.0.8, a fix was added that corrected updates of deeply nested children so that they validate. But it did not resolve validation of creation of grandchildren.

      Note the follow reproduction case. Grandchild comment title is set to nil, but comment requires title to be present. Also, post_id is needed, but not supplied at all. Despite these, update! does not raise an error for either, and the comment is silently skipped.

      require 'bundler/inline'
      
      gemfile do
        source 'https://rubygems.org'
        gem 'mongoid', '9.0.8'
      end
      
      Mongoid.configure do |config|
        config.clients.default = {
          hosts: ['localhost:27017'],
          database: 'mongoid_test',
        }
      end
      
      class Post
        include Mongoid::Document
      
        has_many :comments, inverse_of: :post
      
        accepts_nested_attributes_for :comments, allow_destroy: true
      
        field :title, type: String
      end
      
      class Comment
        include Mongoid::Document
      
        belongs_to :post, inverse_of: :comments
      
        belongs_to :parent, class_name: 'Comment', inverse_of: :comments, optional: true
      
        has_many :comments, inverse_of: :parent
      
        accepts_nested_attributes_for :comments, allow_destroy: true
      
        validates :title, presence: true
      
        field :title, type: String
      end
      
      post = Post.create(title: 'Post 1')
      comment1 = post.comments.create!(title: 'Comment 1')
      
      puts 'Before:'
      puts post.inspect
      puts "  #{comment1.inspect}"
      puts "    #{comment1.comments.to_a}"
      puts ''
      
      post.update!(
        comments_attributes: [
          {
            _id: comment1.id,
            comments_attributes: [
              {
                title: nil
              }
            ]
          }
        ]
      )
      
      puts 'After:'
      puts post.reload.inspect
      puts "  #{comment1.reload.inspect}"
      puts "    #{comment1.comments.to_a}"
      

            Assignee:
            Jamis Buck
            Reporter:
            Kieran Pilkington
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: