mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Fix nested include attributes
When the requests asked for a nested attribute in the `include` and it's missing or empty, don't break because the type of the object can't be determined. If the request is for a collection and none of the elements has the attribute, it will not be added to the `linked` key, similar to what happens with simple includes.
This commit is contained in:
parent
22202a115a
commit
1d7d9fd6aa
@ -68,8 +68,8 @@ module ActiveModel
|
|||||||
|
|
||||||
resource_path = [parent, resource_name].compact.join('.')
|
resource_path = [parent, resource_name].compact.join('.')
|
||||||
|
|
||||||
if include_assoc?(resource_path)
|
if include_assoc?(resource_path) && resource_type = serialized_object_type(serializers)
|
||||||
plural_name = serialized_object_type(serializers).pluralize.to_sym
|
plural_name = resource_type.pluralize.to_sym
|
||||||
@top[:linked] ||= {}
|
@top[:linked] ||= {}
|
||||||
@top[:linked][plural_name] ||= []
|
@top[:linked][plural_name] ||= []
|
||||||
|
|
||||||
|
|||||||
@ -26,6 +26,9 @@ module ActionController
|
|||||||
@first_comment.author = @author2
|
@first_comment.author = @author2
|
||||||
@second_comment.post = @post
|
@second_comment.post = @post
|
||||||
@second_comment.author = nil
|
@second_comment.author = nil
|
||||||
|
@post2 = Post.new(id: 2, title: "Another Post", body: "Body")
|
||||||
|
@post2.author = @author
|
||||||
|
@post2.comments = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_resource_without_include
|
def render_resource_without_include
|
||||||
@ -48,6 +51,18 @@ module ActionController
|
|||||||
render json: @post, include: 'author,author.roles', adapter: :json_api
|
render json: @post, include: 'author,author.roles', adapter: :json_api
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_resource_with_missing_nested_has_many_include
|
||||||
|
setup_post
|
||||||
|
@post.author = @author2 # author2 has no roles.
|
||||||
|
render json: @post, include: 'author,author.roles', adapter: :json_api
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_collection_with_missing_nested_has_many_include
|
||||||
|
setup_post
|
||||||
|
@post.author = @author2
|
||||||
|
render json: [@post, @post2], include: 'author,author.roles', adapter: :json_api
|
||||||
|
end
|
||||||
|
|
||||||
def render_collection_without_include
|
def render_collection_without_include
|
||||||
setup_post
|
setup_post
|
||||||
render json: [@post], adapter: :json_api
|
render json: [@post], adapter: :json_api
|
||||||
@ -124,6 +139,20 @@ module ActionController
|
|||||||
response = JSON.parse(@response.body)
|
response = JSON.parse(@response.body)
|
||||||
assert response.key? 'linked'
|
assert response.key? 'linked'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_resource_with_nested_attributes_even_when_missing_associations
|
||||||
|
get :render_resource_with_missing_nested_has_many_include
|
||||||
|
response = JSON.parse(@response.body)
|
||||||
|
assert response.key? 'linked'
|
||||||
|
refute response['linked'].key? 'roles'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_collection_with_missing_nested_has_many_include
|
||||||
|
get :render_collection_with_missing_nested_has_many_include
|
||||||
|
response = JSON.parse(@response.body)
|
||||||
|
assert response.key? 'linked'
|
||||||
|
assert response['linked'].key? 'roles'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user