Skip to content

Commit 34d6571

Browse files
committed
Merge pull request #1214 from NullVoxPopuli/issue/1211-failing-test
Fix #1211, include_tree is null when using the key: options
2 parents b8a5bbd + f8323fc commit 34d6571

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Features:
2020
- [#1050](https://github.com/rails-api/active_model_serializers/pull/1050) Add support for toplevel jsonapi member (@beauby, @bf4)
2121

2222
Fixes:
23+
- [#1214](https://github.com/rails-api/active_model_serializers/pull/1214) retrieve the key from the reflection options when building associations (@NullVoxPopuli, @hut8)
2324

2425
Misc:
2526
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)

lib/active_model/serializer/associations.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def associations(include_tree = DEFAULT_INCLUDE_TREE)
9292

9393
Enumerator.new do |y|
9494
self.class._reflections.each do |reflection|
95-
next unless include_tree.key?(reflection.name)
95+
key = reflection.options.fetch(:key, reflection.name)
96+
next unless include_tree.key?(key)
9697
y.yield reflection.build_association(self, instance_options)
9798
end
9899
end

test/action_controller/json_api/linked_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def render_resource_with_include
4646
render json: @post, include: [:author], adapter: :json_api
4747
end
4848

49+
def render_resource_with_include_of_custom_key_by_original
50+
setup_post
51+
render json: @post, include: [:reviews], adapter: :json_api, serializer: PostWithCustomKeysSerializer
52+
end
53+
4954
def render_resource_with_nested_include
5055
setup_post
5156
render json: @post, include: [comments: [:author]], adapter: :json_api
@@ -137,6 +142,18 @@ def test_render_resource_with_nested_has_many_include
137142
assert_equal expected_linked, response['included']
138143
end
139144

145+
def test_render_resource_with_include_of_custom_key_by_original
146+
get :render_resource_with_include_of_custom_key_by_original
147+
response = JSON.parse(@response.body)
148+
assert response.key? 'included'
149+
150+
relationships = response['data']['relationships']
151+
152+
assert_includes relationships, 'reviews'
153+
assert_includes relationships, 'writer'
154+
assert_includes relationships, 'site'
155+
end
156+
140157
def test_render_resource_with_nested_include
141158
get :render_resource_with_nested_include
142159
response = JSON.parse(@response.body)

0 commit comments

Comments
 (0)