mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Add config.serializer_lookup_enabled that defaults true
This commit is contained in:
parent
47a14b6581
commit
28394340d8
@ -21,8 +21,10 @@ Features:
|
|||||||
- [#1127](https://github.com/rails-api/active_model_serializers/pull/1127) Add support for nested
|
- [#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).
|
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)
|
- [#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)
|
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:
|
Fixes:
|
||||||
- [#1239](https://github.com/rails-api/active_model_serializers/pull/1239) Fix duplicates in JSON API compound documents (@beauby)
|
- [#1239](https://github.com/rails-api/active_model_serializers/pull/1239) Fix duplicates in JSON API compound documents (@beauby)
|
||||||
|
|||||||
@ -5,7 +5,7 @@ The following configuration options can be set on `ActiveModel::Serializer.confi
|
|||||||
## General
|
## General
|
||||||
|
|
||||||
- `adapter`: The [adapter](adapters.md) to use. Possible values: `:attributes, :json, :json_api`. Default: `:attributes`.
|
- `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
|
## JSON API
|
||||||
|
|
||||||
|
|||||||
@ -27,10 +27,6 @@ module ActionController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get_serializer(resource, options = {})
|
def get_serializer(resource, options = {})
|
||||||
unless options[:serializer] || options[:each_serializer] || ActiveModel::Serializer.config.automatic_lookup
|
|
||||||
return resource
|
|
||||||
end
|
|
||||||
|
|
||||||
if !use_adapter?
|
if !use_adapter?
|
||||||
warn 'ActionController::Serialization#use_adapter? has been removed. '\
|
warn 'ActionController::Serialization#use_adapter? has been removed. '\
|
||||||
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
|
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
|
||||||
|
|||||||
@ -208,6 +208,7 @@ module ActiveModel
|
|||||||
# 2. try again with superclass, if present
|
# 2. try again with superclass, if present
|
||||||
# 3. nil
|
# 3. nil
|
||||||
def self.get_serializer_for(klass)
|
def self.get_serializer_for(klass)
|
||||||
|
return nil unless config.serializer_lookup_enabled
|
||||||
serializers_cache.fetch_or_store(klass) do
|
serializers_cache.fetch_or_store(klass) do
|
||||||
# NOTE(beauby): When we drop 1.9.3 support we can lazify the map for perfs.
|
# 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 }
|
serializer_class = serializer_lookup_chain_for(klass).map(&:safe_constantize).find { |x| x && x < ActiveModel::Serializer }
|
||||||
|
|||||||
@ -9,6 +9,7 @@ module ActiveModel
|
|||||||
included do |base|
|
included do |base|
|
||||||
config = base.config
|
config = base.config
|
||||||
config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
|
config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
|
||||||
|
config.serializer_lookup_enabled = true
|
||||||
|
|
||||||
def config.array_serializer=(collection_serializer)
|
def config.array_serializer=(collection_serializer)
|
||||||
self.collection_serializer = collection_serializer
|
self.collection_serializer = collection_serializer
|
||||||
@ -20,7 +21,6 @@ module ActiveModel
|
|||||||
|
|
||||||
config.adapter = :attributes
|
config.adapter = :attributes
|
||||||
config.jsonapi_resource_type = :plural
|
config.jsonapi_resource_type = :plural
|
||||||
config.automatic_lookup = true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -57,7 +57,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_serializer_for_non_ams_serializer
|
def test_serializer_for_non_ams_serializer
|
||||||
serializer = ActiveModel::Serializer.serializer_for(@tweet)
|
serializer = ActiveModel::Serializer.serializer_for(@tweet)
|
||||||
assert_nil(serializer)
|
assert_equal nil, serializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_serializer_for_existing_serializer
|
def test_serializer_for_existing_serializer
|
||||||
@ -65,6 +65,13 @@ module ActiveModel
|
|||||||
assert_equal ProfileSerializer, serializer
|
assert_equal ProfileSerializer, serializer
|
||||||
end
|
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
|
def test_serializer_for_not_existing_serializer
|
||||||
serializer = ActiveModel::Serializer.serializer_for(@model)
|
serializer = ActiveModel::Serializer.serializer_for(@model)
|
||||||
assert_equal nil, serializer
|
assert_equal nil, serializer
|
||||||
@ -75,21 +82,51 @@ module ActiveModel
|
|||||||
assert_equal ProfileSerializer, serializer
|
assert_equal ProfileSerializer, serializer
|
||||||
end
|
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
|
def test_serializer_custom_serializer
|
||||||
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
|
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
|
||||||
assert_equal ProfileSerializer, serializer
|
assert_equal ProfileSerializer, serializer
|
||||||
end
|
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
|
def test_serializer_for_namespaced_resource
|
||||||
post = ResourceNamespace::Post.new
|
post = ResourceNamespace::Post.new
|
||||||
serializer = ActiveModel::Serializer.serializer_for(post)
|
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
|
end
|
||||||
|
|
||||||
def test_serializer_for_nested_resource
|
def test_serializer_for_nested_resource
|
||||||
comment = ResourceNamespace::Comment.new
|
comment = ResourceNamespace::Comment.new
|
||||||
serializer = ResourceNamespace::PostSerializer.serializer_for(comment)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -31,6 +31,14 @@ module SerializationTesting
|
|||||||
ActiveModel::Serializer.config.replace(old_config)
|
ActiveModel::Serializer.config.replace(old_config)
|
||||||
end
|
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 = {})
|
def serializable(resource, options = {})
|
||||||
ActiveModel::SerializableResource.new(resource, options)
|
ActiveModel::SerializableResource.new(resource, options)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user