From ca3e02edee8789b44282696f901f203d48414b0a Mon Sep 17 00:00:00 2001 From: Johnny Shields <27655+johnnyshields@users.noreply.github.com> Date: Tue, 9 Apr 2024 05:57:32 +0900 Subject: [PATCH] Remove deprecated Document#as_json hack (#5755) Co-authored-by: Jamis Buck --- docs/release-notes/mongoid-9.0.txt | 5 +++++ lib/mongoid/document.rb | 27 --------------------------- lib/mongoid/warnings.rb | 1 - spec/mongoid/criteria_spec.rb | 1 - spec/mongoid/document_spec.rb | 29 ----------------------------- 5 files changed, 5 insertions(+), 58 deletions(-) diff --git a/docs/release-notes/mongoid-9.0.txt b/docs/release-notes/mongoid-9.0.txt index 2845f10c0b..653eb4dc20 100644 --- a/docs/release-notes/mongoid-9.0.txt +++ b/docs/release-notes/mongoid-9.0.txt @@ -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 `_ 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 diff --git a/lib/mongoid/document.rb b/lib/mongoid/document.rb index e55d932c01..cf70ff9552 100644 --- a/lib/mongoid/document.rb +++ b/lib/mongoid/document.rb @@ -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. # diff --git a/lib/mongoid/warnings.rb b/lib/mongoid/warnings.rb index 0a41abf3e9..79333dcfcf 100644 --- a/lib/mongoid/warnings.rb +++ b/lib/mongoid/warnings.rb @@ -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.' diff --git a/spec/mongoid/criteria_spec.rb b/spec/mongoid/criteria_spec.rb index 69d1dbb3f6..65d1ca8092 100644 --- a/spec/mongoid/criteria_spec.rb +++ b/spec/mongoid/criteria_spec.rb @@ -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 diff --git a/spec/mongoid/document_spec.rb b/spec/mongoid/document_spec.rb index 938ac8a8fa..03915b1669 100644 --- a/spec/mongoid/document_spec.rb +++ b/spec/mongoid/document_spec.rb @@ -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