Inconsistent in-memory attribute serialization after update! depending on legacy_attributes setting

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: Rails
    • None
    • None
    • Ruby Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      When updating a field containing an array of hashes using `update!`, the in-memory representation of the updated value differs depending on the `legacy_attributes` configuration.

      Specifically:

      • `legacy_attributes=true`: the updated value appears immediately in a normalized (string-keyed) form.
      • `legacy_attributes=false`: symbol keys are retained in memory.

      Is this an intended change?

      Steps to Reproduce:

      class Request
        include Mongoid::Document
        field :params, type: Array
      end

      In Rails console

      Request.create!
      req = Request.first
      
      # Legacy attributes: false
      req.update!(params: [{ date_from: "2025-05-01", date_to: "2025-05-31" }, 1, 100])
      req.params
      => [{:date_from=>"2025-05-01", :date_to=>"2025-05-31"}, 1, 100] # Symbol key
      
      # Legacy attributes: true
      req.update!(params: [{ date_from: "2025-05-01", date_to: "2025-05-31" }, 1, 100])
      req.params
      => [{"date_from"=>"2025-05-01", "date_to"=>"2025-05-31"}, 1, 100] # String key

      Observed Behavior:


      • With legacy_attributes: false, the updated value in memory retains symbol keys and reflects the exact structure passed to update!.
      • With legacy_attributes: true, the updated value in memory is immediately normalized to string keys — as though the document had been reloaded from the database. This suggests an implicit reload or internal refresh of attributes is occurring.

      Expected Behavior:

      • We expect that the in-memory representation of updated attributes remains consistent regardless of the legacy_attributes setting.
      • We expect that values passed to update! are consistently normalized (mongoized) before being returned through accessors like `req.params`.

      Environment:

      • Mongoid version: 8.1.10
      • MongoDB version: 7.0.11
      • mongo-ruby-driver version: 2.21.0
      • Ruby version: 3.3.6
      • Rails version: 7.2.2.1

            Assignee:
            Jamis Buck
            Reporter:
            Jin Oketani
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: