Skip to content

Commit 7b0a85f

Browse files
committed
Merge pull request #936 from insphire/fix-meta-with-custom-root
Include meta when using json adapter with custom root
2 parents 4c7442d + d34bba0 commit 7b0a85f

File tree

4 files changed

+75
-8
lines changed

4 files changed

+75
-8
lines changed

lib/active_model/serializer/adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def meta_key
7575
end
7676

7777
def root
78-
serializer.json_key
78+
@options.fetch(:root) { serializer.json_key }
7979
end
8080

8181
def include_meta(json)

lib/active_model/serializer/adapter/json.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ def serializable_hash(options = {})
3737
@result = @core.merge @hash
3838
end
3939

40-
if root = options.fetch(:root, serializer.json_key)
40+
if root
4141
@result = { root => @result }
42+
else
43+
@result
4244
end
43-
@result
4445
end
4546
end
4647

@@ -49,4 +50,4 @@ def fragment_cache(cached_hash, non_cached_hash)
4950
end
5051
end
5152
end
52-
end
53+
end

test/action_controller/serialization_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def render_using_custom_root
1414
render json: @profile, root: "custom_root"
1515
end
1616

17+
def render_using_custom_root_and_meta
18+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
19+
render json: @profile, root: "custom_root", meta: { total: 10 }
20+
end
21+
1722
def render_using_default_adapter_root
1823
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
1924
# JSON-API adapter sets root by default
@@ -28,6 +33,14 @@ def render_using_custom_root_in_adapter_with_a_default
2833
render json: @profile, root: "profile", adapter: :json_api
2934
end
3035

36+
def render_array_using_custom_root_and_meta
37+
array = [
38+
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
39+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
40+
]
41+
render json: array, root: "custom_root", meta: { total: 10 }
42+
end
43+
3144
def render_array_using_implicit_serializer
3245
array = [
3346
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
@@ -161,6 +174,13 @@ def test_render_using_custom_root
161174
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"}}', @response.body
162175
end
163176

177+
def test_render_using_custom_root_and_meta
178+
get :render_using_custom_root_and_meta
179+
180+
assert_equal 'application/json', @response.content_type
181+
assert_equal '{"custom_root":{"name":"Name 1","description":"Description 1"},"meta":{"total":10}}', @response.body
182+
end
183+
164184
def test_render_using_default_root
165185
get :render_using_default_adapter_root
166186

@@ -197,6 +217,25 @@ def test_render_using_custom_root_in_adapter_with_a_default
197217
assert_equal expected.to_json, @response.body
198218
end
199219

220+
def test_render_array_using_custom_root_and_meta
221+
get :render_array_using_custom_root_and_meta
222+
assert_equal 'application/json', @response.content_type
223+
224+
expected = { custom_root: [
225+
{
226+
name: 'Name 1',
227+
description: 'Description 1',
228+
},
229+
{
230+
name: 'Name 2',
231+
description: 'Description 2',
232+
}],
233+
meta: { total: 10 }
234+
}
235+
236+
assert_equal expected.to_json, @response.body
237+
end
238+
200239
def test_render_array_using_implicit_serializer
201240
get :render_array_using_implicit_serializer
202241
assert_equal 'application/json', @response.content_type

test/serializers/meta_test.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def test_meta_key_is_used
4848
assert_equal expected, adapter.as_json
4949
end
5050

51-
def test_meta_is_not_used_on_arrays
52-
serializer = ArraySerializer.new([@blog], root: "blog", meta: {total: 10}, meta_key: "haha_meta")
51+
def test_meta_is_not_present_on_arrays_without_root
52+
serializer = ArraySerializer.new([@blog], meta: {total: 10})
5353
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
5454
expected = [{
5555
id: 1,
@@ -67,11 +67,38 @@ def test_meta_is_not_used_on_arrays
6767
assert_equal expected, adapter.as_json
6868
end
6969

70+
def test_meta_is_present_on_arrays_with_root
71+
serializer = ArraySerializer.new([@blog], meta: {total: 10}, meta_key: "haha_meta")
72+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, root: 'blog')
73+
expected = {
74+
'blog' => [{
75+
id: 1,
76+
name: "AMS Hints",
77+
writer: {
78+
id: 2,
79+
name: "Steve"
80+
},
81+
articles: [{
82+
id: 3,
83+
title: "AMS",
84+
body: nil
85+
}]
86+
}],
87+
'haha_meta' => {
88+
total: 10
89+
}
90+
}
91+
assert_equal expected, adapter.as_json
92+
end
93+
7094
private
7195

7296
def load_adapter(options)
73-
serializer = AlternateBlogSerializer.new(@blog, options)
74-
ActiveModel::Serializer::Adapter::Json.new(serializer)
97+
adapter_opts, serializer_opts =
98+
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
99+
100+
serializer = AlternateBlogSerializer.new(@blog, serializer_opts)
101+
ActiveModel::Serializer::Adapter::Json.new(serializer, adapter_opts)
75102
end
76103
end
77104
end

0 commit comments

Comments
 (0)