mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Fix namespace lookup for collections and has_many (#1973)
This commit is contained in:
parent
c9a96a05ed
commit
d0de53cbb2
@ -6,6 +6,7 @@ Breaking changes:
|
||||
|
||||
Fixes:
|
||||
|
||||
- [#1973](https://github.com/rails-api/active_model_serializers/pull/1973) Fix namespace lookup for collections and has_many relationships (@groyoh)
|
||||
- [#1887](https://github.com/rails-api/active_model_serializers/pull/1887) Make the comment reflect what the function does (@johnnymo87)
|
||||
- [#1890](https://github.com/rails-api/active_model_serializers/issues/1890) Ensure generator inherits from ApplicationSerializer when available (@richmolj)
|
||||
- [#1922](https://github.com/rails-api/active_model_serializers/pull/1922) Make railtie an optional dependency in runtime (@ggpasqualino)
|
||||
|
||||
@ -71,7 +71,9 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def serializer_from_resource(resource, serializer_context_class, options)
|
||||
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
|
||||
serializer_class = options.fetch(:serializer) do
|
||||
serializer_context_class.serializer_for(resource, namespace: options[:namespace])
|
||||
end
|
||||
|
||||
if serializer_class.nil?
|
||||
ActiveModelSerializers.logger.debug "No serializer found for resource: #{resource.inspect}"
|
||||
|
||||
@ -5,6 +5,7 @@ module ActionController
|
||||
class NamespaceLookupTest < ActionController::TestCase
|
||||
class Book < ::Model; end
|
||||
class Page < ::Model; end
|
||||
class Chapter < ::Model; end
|
||||
class Writer < ::Model; end
|
||||
|
||||
module Api
|
||||
@ -19,6 +20,13 @@ module ActionController
|
||||
attributes :title, :body
|
||||
|
||||
belongs_to :writer
|
||||
has_many :chapters
|
||||
end
|
||||
|
||||
class ChapterSerializer < ActiveModel::Serializer
|
||||
attribute :title do
|
||||
"Chapter - #{object.title}"
|
||||
end
|
||||
end
|
||||
|
||||
class WriterSerializer < ActiveModel::Serializer
|
||||
@ -32,7 +40,22 @@ module ActionController
|
||||
|
||||
def implicit_namespaced_serializer
|
||||
writer = Writer.new(name: 'Bob')
|
||||
book = Book.new(title: 'New Post', body: 'Body', writer: writer)
|
||||
book = Book.new(title: 'New Post', body: 'Body', writer: writer, chapters: [])
|
||||
|
||||
render json: book
|
||||
end
|
||||
|
||||
def implicit_namespaced_collection_serializer
|
||||
chapter1 = Chapter.new(title: 'Oh')
|
||||
chapter2 = Chapter.new(title: 'Oh my')
|
||||
|
||||
render json: [chapter1, chapter2]
|
||||
end
|
||||
|
||||
def implicit_has_many_namespaced_serializer
|
||||
chapter1 = Chapter.new(title: 'Odd World')
|
||||
chapter2 = Chapter.new(title: 'New World')
|
||||
book = Book.new(title: 'New Post', body: 'Body', chapters: [chapter1, chapter2])
|
||||
|
||||
render json: book
|
||||
end
|
||||
@ -84,7 +107,36 @@ module ActionController
|
||||
|
||||
assert_serializer Api::V3::BookSerializer
|
||||
|
||||
expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' } }
|
||||
expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' }, 'chapters' => [] }
|
||||
actual = JSON.parse(@response.body)
|
||||
|
||||
assert_equal expected, actual
|
||||
end
|
||||
|
||||
test 'implicitly uses namespaced serializer for collection' do
|
||||
get :implicit_namespaced_collection_serializer
|
||||
|
||||
assert_serializer 'ActiveModel::Serializer::CollectionSerializer'
|
||||
|
||||
expected = [{ 'title' => 'Chapter - Oh' }, { 'title' => 'Chapter - Oh my' }]
|
||||
actual = JSON.parse(@response.body)
|
||||
|
||||
assert_equal expected, actual
|
||||
end
|
||||
|
||||
test 'implicitly uses namespaced serializer for has_many' do
|
||||
get :implicit_has_many_namespaced_serializer
|
||||
|
||||
assert_serializer Api::V3::BookSerializer
|
||||
|
||||
expected = {
|
||||
'title' => 'New Post',
|
||||
'body' => 'Body', 'writer' => nil,
|
||||
'chapters' => [
|
||||
{ 'title' => 'Chapter - Odd World' },
|
||||
{ 'title' => 'Chapter - New World' }
|
||||
]
|
||||
}
|
||||
actual = JSON.parse(@response.body)
|
||||
|
||||
assert_equal expected, actual
|
||||
|
||||
Loading…
Reference in New Issue
Block a user