Skip to content

Commit d0de53c

Browse files
Yohan RobertNullVoxPopuli
authored andcommitted
Fix namespace lookup for collections and has_many (#1973)
1 parent c9a96a0 commit d0de53c

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Breaking changes:
66

77
Fixes:
88

9+
- [#1973](https://github.com/rails-api/active_model_serializers/pull/1973) Fix namespace lookup for collections and has_many relationships (@groyoh)
910
- [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87)
1011
- [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj)
1112
- [#1922](https://github.com/rails-api/active_model_serializers/pull/1922) Make railtie an optional dependency in runtime (@ggpasqualino)

lib/active_model/serializer/collection_serializer.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def serializers_from_resources
7171
end
7272

7373
def serializer_from_resource(resource, serializer_context_class, options)
74-
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
74+
serializer_class = options.fetch(:serializer) do
75+
serializer_context_class.serializer_for(resource, namespace: options[:namespace])
76+
end
7577

7678
if serializer_class.nil?
7779
ActiveModelSerializers.logger.debug "No serializer found for resource: #{resource.inspect}"

test/action_controller/namespace_lookup_test.rb

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Serialization
55
class NamespaceLookupTest < ActionController::TestCase
66
class Book < ::Model; end
77
class Page < ::Model; end
8+
class Chapter < ::Model; end
89
class Writer < ::Model; end
910

1011
module Api
@@ -19,6 +20,13 @@ class BookSerializer < ActiveModel::Serializer
1920
attributes :title, :body
2021

2122
belongs_to :writer
23+
has_many :chapters
24+
end
25+
26+
class ChapterSerializer < ActiveModel::Serializer
27+
attribute :title do
28+
"Chapter - #{object.title}"
29+
end
2230
end
2331

2432
class WriterSerializer < ActiveModel::Serializer
@@ -32,7 +40,22 @@ class LookupTestController < ActionController::Base
3240

3341
def implicit_namespaced_serializer
3442
writer = Writer.new(name: 'Bob')
35-
book = Book.new(title: 'New Post', body: 'Body', writer: writer)
43+
book = Book.new(title: 'New Post', body: 'Body', writer: writer, chapters: [])
44+
45+
render json: book
46+
end
47+
48+
def implicit_namespaced_collection_serializer
49+
chapter1 = Chapter.new(title: 'Oh')
50+
chapter2 = Chapter.new(title: 'Oh my')
51+
52+
render json: [chapter1, chapter2]
53+
end
54+
55+
def implicit_has_many_namespaced_serializer
56+
chapter1 = Chapter.new(title: 'Odd World')
57+
chapter2 = Chapter.new(title: 'New World')
58+
book = Book.new(title: 'New Post', body: 'Body', chapters: [chapter1, chapter2])
3659

3760
render json: book
3861
end
@@ -84,7 +107,36 @@ def namespace_set_in_before_filter
84107

85108
assert_serializer Api::V3::BookSerializer
86109

87-
expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' } }
110+
expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' }, 'chapters' => [] }
111+
actual = JSON.parse(@response.body)
112+
113+
assert_equal expected, actual
114+
end
115+
116+
test 'implicitly uses namespaced serializer for collection' do
117+
get :implicit_namespaced_collection_serializer
118+
119+
assert_serializer 'ActiveModel::Serializer::CollectionSerializer'
120+
121+
expected = [{ 'title' => 'Chapter - Oh' }, { 'title' => 'Chapter - Oh my' }]
122+
actual = JSON.parse(@response.body)
123+
124+
assert_equal expected, actual
125+
end
126+
127+
test 'implicitly uses namespaced serializer for has_many' do
128+
get :implicit_has_many_namespaced_serializer
129+
130+
assert_serializer Api::V3::BookSerializer
131+
132+
expected = {
133+
'title' => 'New Post',
134+
'body' => 'Body', 'writer' => nil,
135+
'chapters' => [
136+
{ 'title' => 'Chapter - Odd World' },
137+
{ 'title' => 'Chapter - New World' }
138+
]
139+
}
88140
actual = JSON.parse(@response.body)
89141

90142
assert_equal expected, actual

0 commit comments

Comments
 (0)