Make sure we always return an array of hierarchies

Even when we pass an array of only a single object
This commit is contained in:
Bob Van Landuyt 2017-10-13 15:13:52 +02:00
parent a1bfdf76f2
commit 4d79159973
3 changed files with 21 additions and 1 deletions

View File

@ -28,7 +28,9 @@ class GroupChildSerializer < BaseSerializer
represent_hierarchy(children.hierarchy(hierarchy_root), opts).first
else
hierarchies = GroupDescendant.build_hierarchy(children, hierarchy_root)
represent_hierarchy(hierarchies, opts)
# When an array was passed, we always want to represent an array.
# Even if the hierarchy only contains one element
represent_hierarchy(Array.wrap(hierarchies), opts)
end
end

View File

@ -141,6 +141,15 @@ describe Groups::ChildrenController do
expect(response).to have_http_status(200)
end
it 'returns an array with one element when only one result is matched' do
create(:project, :public, namespace: group, name: 'match')
get :index, group_id: group.to_param, filter: 'match', format: :json
expect(json_response).to be_kind_of(Array)
expect(json_response.size).to eq(1)
end
it 'returns an empty array when there are no search results' do
subgroup = create(:group, :public, parent: group)
l2_subgroup = create(:group, :public, parent: subgroup)

View File

@ -95,6 +95,15 @@ describe GroupChildSerializer do
expect(project1_json[:id]).to eq(project1.id)
expect(project2_json[:id]).to eq(project2.id)
end
it 'returns an array when an array of a single instance was given' do
project = create(:project, namespace: parent)
json = serializer.represent([project])
expect(json).to be_kind_of(Array)
expect(json.size).to eq(1)
end
end
end
end