Skip to content

MONGOID-5684 Ensure field_was returns the same type as field for hash-typed fields#6142

Open
jamis wants to merge 1 commit intomongodb:masterfrom
jamis:5684-field_was-hash
Open

MONGOID-5684 Ensure field_was returns the same type as field for hash-typed fields#6142
jamis wants to merge 1 commit intomongodb:masterfrom
jamis:5684-field_was-hash

Conversation

@jamis
Copy link
Copy Markdown
Contributor

@jamis jamis commented Apr 29, 2026

In Mongoid, Hash-typed fields return BSON::Document instances (a special Hash subclass). However, assigning a new hash value to such a field and then querying its previous value (via [field]_was) would return a Hash instance, even though it was actually a BSON::Document instance previously.

This PR fixes that by implementing an appropriate deep copy method for BSON::Document instances.

Copilot AI review requested due to automatic review settings April 29, 2026 21:13
@jamis jamis requested a review from a team as a code owner April 29, 2026 21:13
@jamis jamis requested a review from comandeo-mongo April 29, 2026 21:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Mongoid dirty-tracking so that [field]_was preserves BSON::Document for Hash-typed fields (matching the field getter), by adding a BSON::Document#__deep_copy__ implementation and a regression spec.

Changes:

  • Add BSON::Document#__deep_copy__ that deep-copies while preserving the BSON::Document type.
  • Load the new BSON::Document extension via lib/mongoid/extensions.rb.
  • Add a regression spec asserting map_was returns a BSON::Document and the expected prior value.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
spec/mongoid/changeable_spec.rb Adds regression coverage for *_was behavior on Hash fields stored as BSON::Document.
lib/mongoid/extensions/bson_document.rb Introduces BSON::Document#__deep_copy__ to preserve type during deep copy for dirty tracking.
lib/mongoid/extensions.rb Requires the new BSON::Document extension so it is loaded with other core extensions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread spec/mongoid/changeable_spec.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants