Restrict serializable_hash to accepted options, only for tests

This commit is contained in:
Benjamin Fleischer
2016-04-13 00:30:47 -05:00
parent aad7779a3f
commit 929a5d0a51
7 changed files with 57 additions and 3 deletions

View File

@@ -18,6 +18,8 @@ require 'active_model/serializer/type'
# reified when subclassed to decorate a resource.
module ActiveModel
class Serializer
# @see #serializable_hash for more details on these valid keys.
SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
extend ActiveSupport::Autoload
autoload :Adapter
autoload :Null

View File

@@ -8,7 +8,7 @@ module ActiveModelSerializers
end
def serializable_hash(options = nil)
options ||= {}
options = serialization_options(options)
if serializer.respond_to?(:each)
serializable_hash_for_collection(options)

View File

@@ -19,6 +19,8 @@ module ActiveModelSerializers
@cached_name ||= self.class.name.demodulize.underscore
end
# Subclasses that implement this method must first call
# options = serialization_options(options)
def serializable_hash(_options = nil)
fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
end
@@ -41,6 +43,12 @@ module ActiveModelSerializers
private
# see https://github.com/rails-api/active_model_serializers/pull/965
# When <tt>options</tt> is +nil+, sets it to +{}+
def serialization_options(options)
options ||= {} # rubocop:disable Lint/UselessAssignment
end
def meta
instance_options.fetch(:meta, nil)
end

View File

@@ -2,7 +2,7 @@ module ActiveModelSerializers
module Adapter
class Json < Base
def serializable_hash(options = nil)
options ||= {}
options = serialization_options(options)
serialized_hash = { root => Attributes.new(serializer, instance_options).serializable_hash(options) }
self.class.transform_key_casing!(serialized_hash, instance_options)
end