Skip to content

Commit

Permalink
Remove deprecated Document#as_json hack (#5755)
Browse files Browse the repository at this point in the history
Co-authored-by: Jamis Buck <[email protected]>
  • Loading branch information
johnnyshields and jamis authored Apr 8, 2024
1 parent 3963eba commit ca3e02e
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 58 deletions.
5 changes: 5 additions & 0 deletions docs/release-notes/mongoid-9.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,16 @@ prior has been dropped (you must use a minimum of version 8.0.)
Deprecated functionality removed
--------------------------------

**Breaking change:** The following deprecated functionality is now removed:

- The ``Mongoid::QueryCache`` module has been removed. Please replace any usages 1-for-1 with ``Mongo::QueryCache``.
The method ``Mongoid::QueryCache#clear_cache`` should be replaced with ``Mongo::QueryCache#clear``.
All other methods and submodules are identically named. Refer to the `driver query cache documentation
<https://mongodb.com/docs/ruby-driver/current/reference/query-cache/>`_ for more details.
- ``Object#blank_criteria?`` method is removed (was previously deprecated.)
- ``Document#as_json :compact`` option is removed. Please call ```#compact`` on the
returned ``Hash`` object instead.
- The deprecated class ``Mongoid::Errors::InvalidStorageParent`` has been removed.


``touch`` method now clears changed state
Expand Down
27 changes: 0 additions & 27 deletions lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,33 +135,6 @@ def as_document
BSON::Document.new(as_attributes)
end

# Calls #as_json on the document with additional, Mongoid-specific options.
#
# @note Rails 6 changes return value of as_json for non-primitive types
# such as BSON::ObjectId. In Rails <= 5, as_json returned these as
# instances of the class. In Rails 6, these are returned serialized to
# primitive types (e.g. {'$oid'=>'5bcfc40bde340b37feda98e9'}).
# See https://github.com/rails/rails/commit/2e5cb980a448e7f4ab00df6e9ad4c1cc456616aa
# for more information.
#
# @example Get the document as json.
# document.as_json(compact: true)
#
# @param [ Hash ] options The options.
#
# @option options [ true | false ] :compact (Deprecated) Whether to include fields
# with nil values in the json document.
#
# @return [ Hash ] The document as json.
def as_json(options = nil)
rv = super
if options && options[:compact]
Mongoid::Warnings.warn_as_json_compact_deprecated
rv = rv.compact
end
rv
end

# Returns an instance of the specified class with the attributes,
# errors, and embedded documents of the current document.
#
Expand Down
1 change: 0 additions & 1 deletion lib/mongoid/warnings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def warning(id, message)
end

warning :geo_haystack_deprecated, 'The geoHaystack type is deprecated.'
warning :as_json_compact_deprecated, '#as_json :compact option is deprecated. Please call #compact on the returned Hash object instead.'
warning :symbol_type_deprecated, 'The BSON Symbol type is deprecated by MongoDB. Please use String or StringifiedSymbol field types instead of the Symbol field type.'
warning :legacy_readonly, 'The readonly! method will only mark the document readonly when the legacy_readonly feature flag is switched off.'
warning :mutable_ids, 'Ignoring updates to immutable attribute `_id`. Please set Mongoid::Config.immutable_ids to true and update your code so that `_id` is never updated.'
Expand Down
1 change: 0 additions & 1 deletion spec/mongoid/criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@
Band.where(name: "Depeche Mode")
end


it "returns the criteria as a json hash" do
expect(criteria.as_json).to eq([ band.serializable_hash.as_json ])
end
Expand Down
29 changes: 0 additions & 29 deletions spec/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -429,35 +429,6 @@ class << self; attr_accessor :name; end
end
end
end

context "when the Mongoid-specific options are provided" do

let(:options) do
{ compact: true }
end

it "applies the Mongoid-specific options" do
expect(person.as_json(options)["title"]).to eq("Sir")
expect(person.as_json(options)["age"]).to eq(100)
expect(person.as_json(options).keys).not_to include("lunch_time")
end

context "when options for the super method are provided" do

let(:options) do
{ compact: true, only: [:title, :pets, :ssn] }
end

it "passes the options through to the super method" do
expect(person.as_json(options)["title"]).to eq("Sir")
expect(person.as_json(options)["pets"]).to eq(false)
end

it "applies the Mongoid-specific options" do
expect(person.as_json(options).keys).not_to include("ssn")
end
end
end
end

describe "#as_document" do
Expand Down

0 comments on commit ca3e02e

Please sign in to comment.