diff --git a/lib/active_model/serializer/adapter/fragment_cache.rb b/lib/active_model/serializer/adapter/fragment_cache.rb index 5c97a64a4..02d56eaae 100644 --- a/lib/active_model/serializer/adapter/fragment_cache.rb +++ b/lib/active_model/serializer/adapter/fragment_cache.rb @@ -94,6 +94,10 @@ def fragment_serializer(name, klass) cached.constantize.cache(klass._cache_options) + # Preserve the type setting in the cached/non-cached serializer classes + cached.constantize.type(klass._type) + non_cached.constantize.type(klass._type) + cached.constantize.fragmented(serializer) non_cached.constantize.fragmented(serializer) diff --git a/test/adapter/fragment_cache_test.rb b/test/adapter/fragment_cache_test.rb index 0ad78eb55..aded06d4f 100644 --- a/test/adapter/fragment_cache_test.rb +++ b/test/adapter/fragment_cache_test.rb @@ -3,6 +3,14 @@ module ActiveModel class Serializer module Adapter class FragmentCacheTest < ActiveSupport::TestCase + TypedRoleSerializer = Class.new(ActiveModel::Serializer) do + type 'my-roles' + cache only: [:name], skip_digest: true + attributes :id, :name, :description + + belongs_to :author + end + def setup super @spam = Spam::UnrelatedLink.new(id: 'spam-id-1') @@ -31,8 +39,12 @@ def test_fragment_fetch_with_namespaced_object } assert_equal(@spam_hash.fetch, expected_result) end + + def test_fragment_fetch_with_type_override + serialization = serializable(Role.new(name: 'Another Author'), serializer: TypedRoleSerializer, adapter: :json_api).serializable_hash + assert_equal(TypedRoleSerializer._type, serialization.fetch(:data).fetch(:type)) + end end end end end -