mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Make sure render json: ..., each_serializer: ... is working with Enumerables
If you try to render Enumerable with custom `each_serializer` then it will render with default serializer instead: ```ruby render json: sequel_scope, each_serializer: CustomSerializer ``` This commit fixes this behaviour. Most likely fixes https://github.com/rails-api/active_model_serializers/issues/664 as well
This commit is contained in:
4
test/fixtures/poro.rb
vendored
4
test/fixtures/poro.rb
vendored
@@ -92,6 +92,10 @@ class ProfileSerializer < ActiveModel::Serializer
|
||||
attributes :name, :description
|
||||
end
|
||||
|
||||
class DifferentProfileSerializer < ActiveModel::Serializer
|
||||
attributes :name
|
||||
end
|
||||
|
||||
class CategorySerializer < ActiveModel::Serializer
|
||||
attributes :name
|
||||
|
||||
|
||||
@@ -283,5 +283,21 @@ module ActionController
|
||||
assert_equal("{\"my\":[{\"name\":\"Name 1\",\"email\":\"mail@server.com\",\"profile_id\":#{@controller.user.profile.object_id}}],\"profiles\":[{\"name\":\"N1\",\"description\":\"D1\"}]}", @response.body)
|
||||
end
|
||||
end
|
||||
|
||||
class ExplicitEachSerializerWithEnumarableObjectTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_array
|
||||
render json: [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })].to_enum, each_serializer: DifferentProfileSerializer
|
||||
end
|
||||
end
|
||||
|
||||
tests MyController
|
||||
|
||||
def test_render_array
|
||||
get :render_array
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal '{"my":[{"name":"Name 1"}]}', @response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user