Add config.serializer_lookup_enabled that defaults true

This commit is contained in:
Benjamin Fleischer 2015-11-30 00:12:21 -06:00
parent 47a14b6581
commit 28394340d8
7 changed files with 54 additions and 10 deletions

View File

@ -21,8 +21,10 @@ Features:
- [#1127](https://github.com/rails-api/active_model_serializers/pull/1127) Add support for nested
associations for JSON and Attributes adapters via the `include` option (@NullVoxPopuli, @beauby).
- [#1050](https://github.com/rails-api/active_model_serializers/pull/1050) Add support for toplevel jsonapi member (@beauby, @bf4)
- [#tbd](https://github.com/rails-api/active_model_serializers/pull/tbd) Rename ArraySerializer to
- [#1251](https://github.com/rails-api/active_model_serializers/pull/1251) Rename ArraySerializer to
CollectionSerializer for clarity, add ActiveModelSerializers.config.collection_serializer (@bf4)
- [#1295](https://github.com/rails-api/active_model_serializers/pull/1295) Add config `serializer_lookup_enabled` that,
when disabled, requires serializers to explicitly specified. (@trek)
Fixes:
- [#1239](https://github.com/rails-api/active_model_serializers/pull/1239) Fix duplicates in JSON API compound documents (@beauby)

View File

@ -5,7 +5,7 @@ The following configuration options can be set on `ActiveModel::Serializer.confi
## General
- `adapter`: The [adapter](adapters.md) to use. Possible values: `:attributes, :json, :json_api`. Default: `:attributes`.
- `automatic_lookup`: Whether serializer should be automatically looked up or manually provided. Default: `true`
- `serializer_lookup_enabled`: When `false`, serializers must be explicitly specified. Default: `true`
## JSON API

View File

@ -27,10 +27,6 @@ module ActionController
end
def get_serializer(resource, options = {})
unless options[:serializer] || options[:each_serializer] || ActiveModel::Serializer.config.automatic_lookup
return resource
end
if !use_adapter?
warn 'ActionController::Serialization#use_adapter? has been removed. '\
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"

View File

@ -208,6 +208,7 @@ module ActiveModel
# 2. try again with superclass, if present
# 3. nil
def self.get_serializer_for(klass)
return nil unless config.serializer_lookup_enabled
serializers_cache.fetch_or_store(klass) do
# NOTE(beauby): When we drop 1.9.3 support we can lazify the map for perfs.
serializer_class = serializer_lookup_chain_for(klass).map(&:safe_constantize).find { |x| x && x < ActiveModel::Serializer }

View File

@ -9,6 +9,7 @@ module ActiveModel
included do |base|
config = base.config
config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
config.serializer_lookup_enabled = true
def config.array_serializer=(collection_serializer)
self.collection_serializer = collection_serializer
@ -20,7 +21,6 @@ module ActiveModel
config.adapter = :attributes
config.jsonapi_resource_type = :plural
config.automatic_lookup = true
end
end
end

View File

@ -57,7 +57,7 @@ module ActiveModel
def test_serializer_for_non_ams_serializer
serializer = ActiveModel::Serializer.serializer_for(@tweet)
assert_nil(serializer)
assert_equal nil, serializer
end
def test_serializer_for_existing_serializer
@ -65,6 +65,13 @@ module ActiveModel
assert_equal ProfileSerializer, serializer
end
def test_serializer_for_existing_serializer_with_lookup_disabled
serializer = with_serializer_lookup_disabled do
ActiveModel::Serializer.serializer_for(@profile)
end
assert_equal nil, serializer
end
def test_serializer_for_not_existing_serializer
serializer = ActiveModel::Serializer.serializer_for(@model)
assert_equal nil, serializer
@ -75,21 +82,51 @@ module ActiveModel
assert_equal ProfileSerializer, serializer
end
def test_serializer_inherited_serializer_with_lookup_disabled
serializer = with_serializer_lookup_disabled do
ActiveModel::Serializer.serializer_for(@my_profile)
end
assert_equal nil, serializer
end
def test_serializer_custom_serializer
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
assert_equal ProfileSerializer, serializer
end
def test_serializer_custom_serializer_with_lookup_disabled
serializer = with_serializer_lookup_disabled do
ActiveModel::Serializer.serializer_for(@custom_profile)
end
assert_equal ProfileSerializer, serializer
end
def test_serializer_for_namespaced_resource
post = ResourceNamespace::Post.new
serializer = ActiveModel::Serializer.serializer_for(post)
assert_equal(ResourceNamespace::PostSerializer, serializer)
assert_equal ResourceNamespace::PostSerializer, serializer
end
def test_serializer_for_namespaced_resource_with_lookup_disabled
post = ResourceNamespace::Post.new
serializer = with_serializer_lookup_disabled do
ActiveModel::Serializer.serializer_for(post)
end
assert_equal nil, serializer
end
def test_serializer_for_nested_resource
comment = ResourceNamespace::Comment.new
serializer = ResourceNamespace::PostSerializer.serializer_for(comment)
assert_equal(ResourceNamespace::PostSerializer::CommentSerializer, serializer)
assert_equal ResourceNamespace::PostSerializer::CommentSerializer, serializer
end
def test_serializer_for_nested_resource_with_lookup_disabled
comment = ResourceNamespace::Comment.new
serializer = with_serializer_lookup_disabled do
ResourceNamespace::PostSerializer.serializer_for(comment)
end
assert_equal nil, serializer
end
end
end

View File

@ -31,6 +31,14 @@ module SerializationTesting
ActiveModel::Serializer.config.replace(old_config)
end
def with_serializer_lookup_disabled
original_serializer_lookup = ActiveModelSerializers.config.serializer_lookup_enabled
ActiveModelSerializers.config.serializer_lookup_enabled = false
yield
ensure
ActiveModelSerializers.config.serializer_lookup_enabled = original_serializer_lookup
end
def serializable(resource, options = {})
ActiveModel::SerializableResource.new(resource, options)
end