display empty array when not having ancestors

pull/10136/head
ulferts 3 years ago
parent 630cd9f352
commit f882cf9586
No known key found for this signature in database
GPG Key ID: A205708DE1284017
  1. 2
      db/migrate/20220223095355_projects_lft_rgt_index.rb
  2. 18
      lib/api/v3/projects/project_sql_representer.rb
  3. 2
      lib/api/v3/utilities/sql_walker_results.rb
  4. 2
      spec/lib/api/v3/capabilities/capability_sql_representer_rendering_spec.rb
  5. 116
      spec/lib/api/v3/projects/project_sql_representer_renderning_spec.rb
  6. 41
      spec/requests/api/v3/projects/index_resource_spec.rb

@ -1,5 +1,5 @@
class ProjectsLftRgtIndex < ActiveRecord::Migration[6.1]
def change
add_index :projects, %i[lft rgt], unique: true
add_index :projects, %i[lft rgt]
end
end

@ -42,17 +42,27 @@ module API
protected
def ancestors_sql(walker_result)
origin_subselect = if walker_result.page_size
walker_result.filter_scope.limit(walker_result.page_size).offset((walker_result.offset - 1) * walker_result.page_size)
else
walker_result.filter_scope
end
<<-SQL.squish
SELECT id, json_agg(link) ancestors
SELECT id, CASE WHEN count(link) = 0 THEN '[]' ELSE json_agg(link) END ancestors
FROM
(
SELECT
origin.id,
json_build_object('href', format('/api/v3/projects/%s', ancestors.id), 'title', ancestors.name) link
CASE
WHEN ancestors.id IS NOT NULL
THEN json_build_object('href', format('/api/v3/projects/%s', ancestors.id), 'title', ancestors.name)
ELSE NULL
END link
FROM projects origin
JOIN projects ancestors
LEFT OUTER JOIN projects ancestors
ON ancestors.lft < origin.lft AND ancestors.rgt > origin.rgt
WHERE origin.id IN (#{walker_result.filter_scope.limit(walker_result.page_size).offset((walker_result.offset - 1) * walker_result.page_size).select(:id).to_sql})
WHERE origin.id IN (#{origin_subselect.select(:id).to_sql})
ORDER by origin.id, ancestors.lft
) ancestors
GROUP BY id

@ -34,7 +34,7 @@ module API
class SqlWalkerResults
def initialize(scope, url_query:, self_path: nil, replace_map: {})
self.filter_scope = scope.dup
self.projection_scope = scope.dup.reselect("#{scope.model.table_name}.*")
self.projection_scope = scope.dup.distinct(false).reselect("#{scope.model.table_name}.*")
self.ctes = {}
self.self_path = self_path
self.url_query = url_query

@ -62,7 +62,7 @@ describe ::API::V3::Capabilities::CapabilitySqlRepresenter, 'rendering' do
embed: {},
select: { 'id' => {}, '_type' => {}, 'self' => {}, 'action' => {}, 'context' => {}, 'principal' => {} },
current_user: current_user)
.walk(API::V3::Capabilities::CapabilitySqlRepresenter)
.walk(described_class)
.to_json
end

@ -0,0 +1,116 @@
# OpenProject is an open source project management software.
# Copyright (C) 2010-2022 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
require 'spec_helper'
describe ::API::V3::Projects::ProjectSqlRepresenter, 'rendering' do
include ::API::V3::Utilities::PathHelper
let(:scope) do
Project
.where(id: project.id)
end
let(:project) do
create(:project)
end
current_user do
create(:user,
member_in_project: project,
member_with_permissions: [])
end
subject(:json) do
::API::V3::Utilities::SqlRepresenterWalker
.new(scope,
embed: {},
select: { 'id' => {}, '_type' => {}, 'self' => {}, 'name' => {}, 'ancestors' => {} },
current_user: current_user)
.walk(described_class)
.to_json
end
context 'without an ancestor' do
it 'renders as expected' do
expect(json)
.to be_json_eql(
{
id: project.id,
_type: "Project",
name: project.name,
_links: {
ancestors: [],
self: {
href: api_v3_paths.project(project.id),
title: project.name
}
}
}.to_json)
end
end
context 'with an ancestor' do
let!(:parent) do
create(:project).tap do |parent|
project.parent = parent
project.save
end
end
let!(:grandparent) do
create(:project).tap do |grandparent|
parent.parent = grandparent
parent.save
end
end
it 'renders as expected' do
expect(json)
.to be_json_eql(
{
id: project.id,
_type: "Project",
name: project.name,
_links: {
ancestors: [
{
href: api_v3_paths.project(grandparent.id),
title: grandparent.name
},
{
href: api_v3_paths.project(parent.id),
title: parent.name
}
],
self: {
href: api_v3_paths.project(project.id),
title: project.name
}
}
}.to_json)
end
end
end

@ -42,6 +42,12 @@ describe 'API v3 Project resource index', type: :request, content_type: :json do
let(:other_project) do
create(:project, public: false)
end
let(:parent_project) do
create(:project, public: false, members: { current_user => role }).tap do |parent|
project.parent = parent
project.save
end
end
let(:role) { create(:role) }
let(:filters) { [] }
let(:get_path) do
@ -83,14 +89,6 @@ describe 'API v3 Project resource index', type: :request, content_type: :json do
context 'when filtering for project by ancestor' do
let(:projects) { [project, other_project, parent_project] }
let(:parent_project) do
parent_project = create(:project, public: false, members: { current_user => role })
project.update_attribute(:parent_id, parent_project.id)
parent_project
end
let(:filters) do
[{ ancestor: { operator: '=', values: [parent_project.id.to_s] } }]
end
@ -228,24 +226,39 @@ describe 'API v3 Project resource index', type: :request, content_type: :json do
end
context 'when signaling the properties to include' do
let(:projects) { [project] }
let(:select) { 'elements/id,elements/name' }
let(:projects) { [project, parent_project] }
let(:select) { 'elements/id,elements/name,elements/ancestors,total' }
let(:get_path) do
api_v3_paths.path_for :projects, select: select
end
it 'is the reduced set of properties of the embedded elements' do
expected = {
let(:expected) do
{
total: 2,
_embedded: {
elements: [
{
id: parent_project.id,
name: parent_project.name,
_links: {
ancestors: []
}
},
{
id: project.id,
name: project.name
name: project.name,
_links: {
ancestors: [
href: api_v3_paths.project(parent_project.id),
title: parent_project.name
]
}
}
]
}
}
end
it 'is the reduced set of properties of the embedded elements' do
expect(last_response.body)
.to be_json_eql(expected.to_json)
end

Loading…
Cancel
Save