Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix namespace lookup for collections and has_many #1973

Merged
merged 1 commit into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/active_model/serializer/collection_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ def serializers_from_resources
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}"
Expand Down
56 changes: 54 additions & 2 deletions test/action_controller/namespace_lookup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Serialization
class NamespaceLookupTest < ActionController::TestCase
class Book < ::Model; end
class Page < ::Model; end
class Chapter < ::Model; end
class Writer < ::Model; end

module Api
Expand All @@ -19,6 +20,13 @@ class BookSerializer < ActiveModel::Serializer
attributes :title, :body

belongs_to :writer
has_many :chapters
end

class ChapterSerializer < ActiveModel::Serializer
attribute :title do
"Chapter - #{object.title}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When AMS doesn't find the serializer it uses resource.as_json. So I added this "Chapter -" part, to make sure that we use the namespaced serializer instead of resource.as_json.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that -- good thinkin'

end
end

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

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
Expand Down Expand Up @@ -84,7 +107,36 @@ def namespace_set_in_before_filter

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
Expand Down