OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
display empty array when not having ancestors
ulfertsf882cf95867d
diff --git a/db/migrate/20220223095355_projects_lft_rgt_index.rb b/db/migrate/20220223095355_projects_lft_rgt_index.rbindex 4e020d92678..4e28f8a2248 100644--- a/db/migrate/20220223095355_projects_lft_rgt_index.rb+++ b/db/migrate/20220223095355_projects_lft_rgt_index.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]endenddiff --git a/lib/api/v3/projects/project_sql_representer.rb b/lib/api/v3/projects/project_sql_representer.rbindex 5f71e622736..de99041e8bd 100644--- a/lib/api/v3/projects/project_sql_representer.rb+++ b/lib/api/v3/projects/project_sql_representer.rb@@ -42,17 +42,27 @@ module APIprotecteddef 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 ancestorsFROM(SELECTorigin.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 linkFROM projects origin- JOIN projects ancestors+ LEFT OUTER JOIN projects ancestorsON 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) ancestorsGROUP BY iddiff --git a/lib/api/v3/utilities/sql_walker_results.rb b/lib/api/v3/utilities/sql_walker_results.rbindex fa28e3ec525..cc1e76091df 100644--- a/lib/api/v3/utilities/sql_walker_results.rb+++ b/lib/api/v3/utilities/sql_walker_results.rb@@ -34,7 +34,7 @@ module APIclass SqlWalkerResultsdef 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_pathself.url_query = url_querydiff --git a/spec/lib/api/v3/capabilities/capability_sql_representer_rendering_spec.rb b/spec/lib/api/v3/capabilities/capability_sql_representer_rendering_spec.rbindex bd0e42d94ef..ff4b2e406a2 100644--- a/spec/lib/api/v3/capabilities/capability_sql_representer_rendering_spec.rb+++ b/spec/lib/api/v3/capabilities/capability_sql_representer_rendering_spec.rb@@ -62,7 +62,7 @@ describe ::API::V3::Capabilities::CapabilitySqlRepresenter, 'rendering' doembed: {},select: { 'id' => {}, '_type' => {}, 'self' => {}, 'action' => {}, 'context' => {}, 'principal' => {} },current_user: current_user)- .walk(API::V3::Capabilities::CapabilitySqlRepresenter)+ .walk(described_class).to_jsonenddiff --git a/spec/lib/api/v3/projects/project_sql_representer_renderning_spec.rb b/spec/lib/api/v3/projects/project_sql_representer_renderning_spec.rbnew file mode 100644index 00000000000..3375ce2e5db--- /dev/null+++ b/spec/lib/api/v3/projects/project_sql_representer_renderning_spec.rb@@ -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+enddiff --git a/spec/requests/api/v3/projects/index_resource_spec.rb b/spec/requests/api/v3/projects/index_resource_spec.rbindex 72611f49202..e615d5446a9 100644--- a/spec/requests/api/v3/projects/index_resource_spec.rb+++ b/spec/requests/api/v3/projects/index_resource_spec.rb@@ -42,6 +42,12 @@ describe 'API v3 Project resource index', type: :request, content_type: :json dolet(:other_project) docreate(: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+ endlet(:role) { create(:role) }let(:filters) { [] }let(:get_path) do@@ -83,14 +89,6 @@ describe 'API v3 Project resource index', type: :request, content_type: :json docontext 'when filtering for project by ancestor' dolet(: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 doendcontext '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) doapi_v3_paths.path_for :projects, select: selectend-- 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' doexpect(last_response.body).to be_json_eql(expected.to_json)end