mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 06:46:50 +00:00
Merge pull request #682 from ride/json-api-root
Include root by default in JSON-API serializers
This commit is contained in:
commit
efea975419
@ -2,6 +2,11 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class Adapter
|
class Adapter
|
||||||
class JsonApi < Adapter
|
class JsonApi < Adapter
|
||||||
|
def initialize(serializer, options = {})
|
||||||
|
super
|
||||||
|
serializer.root ||= true
|
||||||
|
end
|
||||||
|
|
||||||
def serializable_hash(opts = {})
|
def serializable_hash(opts = {})
|
||||||
@hash = serializer.attributes
|
@hash = serializer.attributes
|
||||||
|
|
||||||
|
|||||||
@ -13,6 +13,16 @@ module ActionController
|
|||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
render json: @profile, root: "custom_root"
|
render json: @profile, root: "custom_root"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_using_default_adapter_root
|
||||||
|
old_adapter = ActiveModel::Serializer.config.adapter
|
||||||
|
# JSON-API adapter sets root by default
|
||||||
|
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
|
||||||
|
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||||
|
render json: @profile
|
||||||
|
ensure
|
||||||
|
ActiveModel::Serializer.config.adapter = old_adapter
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
tests MyController
|
tests MyController
|
||||||
@ -31,6 +41,13 @@ module ActionController
|
|||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body
|
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_using_default_root
|
||||||
|
get :render_using_default_adapter_root
|
||||||
|
|
||||||
|
assert_equal 'application/json', @response.content_type
|
||||||
|
assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user