mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +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:
@@ -26,6 +26,9 @@ module ActionController
|
||||
@first_comment.author = @author2
|
||||
@second_comment.post = @post
|
||||
@second_comment.author = nil
|
||||
@post2 = Post.new(id: 2, title: "Another Post", body: "Body")
|
||||
@post2.author = @author
|
||||
@post2.comments = []
|
||||
end
|
||||
|
||||
def render_resource_without_include
|
||||
@@ -48,6 +51,18 @@ module ActionController
|
||||
render json: @post, include: 'author,author.roles', adapter: :json_api
|
||||
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
|
||||
setup_post
|
||||
render json: [@post], adapter: :json_api
|
||||
@@ -124,6 +139,20 @@ module ActionController
|
||||
response = JSON.parse(@response.body)
|
||||
assert response.key? 'linked'
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user