Skip to content

Commit 1a42345

Browse files
committed
Only use subclasses of ActiveModel::Serializer during lookup.
1 parent f3403c3 commit 1a42345

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/active_model/serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def self.serializer_lookup_chain_for(klass)
202202
def self.get_serializer_for(klass)
203203
serializers_cache.fetch_or_store(klass) do
204204
# NOTE(beauby): When we drop 1.9.3 support we can lazify the map for perfs.
205-
serializer_class = serializer_lookup_chain_for(klass).map(&:safe_constantize).find { |x| x }
205+
serializer_class = serializer_lookup_chain_for(klass).map(&:safe_constantize).find { |x| x && x < ActiveModel::Serializer }
206206

207207
if serializer_class
208208
serializer_class

test/serializers/serializer_for_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,20 @@ class CustomProfile
4444
def serializer_class; ProfileSerializer; end
4545
end
4646

47+
Tweet = Class.new(::Model)
48+
TweetSerializer = Class.new
49+
4750
def setup
4851
@profile = Profile.new
4952
@my_profile = MyProfile.new
5053
@custom_profile = CustomProfile.new
5154
@model = ::Model.new
55+
@tweet = Tweet.new
56+
end
57+
58+
def test_serializer_for_non_ams_serializer
59+
serializer = ActiveModel::Serializer.serializer_for(@tweet)
60+
assert_nil(serializer)
5261
end
5362

5463
def test_serializer_for_existing_serializer

0 commit comments

Comments
 (0)