Skip to content

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

stabilize selecting no elements on collection

ulfertsf8066b27f96d
diff --git a/lib/api/decorators/sql_collection_representer.rb b/lib/api/decorators/sql_collection_representer.rb
index 3150324494e..43bc30e62c7 100644
--- a/lib/api/decorators/sql_collection_representer.rb
+++ b/lib/api/decorators/sql_collection_representer.rb
@@ -95,7 +95,9 @@ module API
embedded :elements,
representation: ->(walker_result) do
- "json_agg(#{walker_result.replace_map['elements']})"
+ replacement = walker_result.replace_map['elements']
+
+ replacement ? "json_agg(#{replacement})" : 'null'
end
end
end
diff --git a/lib/api/v3/utilities/sql_representer_walker.rb b/lib/api/v3/utilities/sql_representer_walker.rb
index 685a8ebe407..6aee56425a6 100644
--- a/lib/api/v3/utilities/sql_representer_walker.rb
+++ b/lib/api/v3/utilities/sql_representer_walker.rb
@@ -63,7 +63,7 @@ module API
result.projection_scope = current_representer.joins(select_for(stack), result.projection_scope)
end
- embedded_depth_first([], start) do |_, _, current_representer|
+ embedded_depth_first([], start) do |_, stack, current_representer|
result.ctes.merge!(current_representer.ctes(result))
end
@@ -96,15 +96,15 @@ module API
up_map[key] = embedded_depth_first(stack.dup << key, representer, &block)
end
- yield up_map, stack, current_representer
+ yield up_map, stack, current_representer if select_for(stack)
end
def select_for(stack)
stack.any? ? select.dig(*stack) : select
end
- def embed_for(stack)
- stack.any? ? embed.dig(*stack) : embed
+ def embed_for(stacker)
+ stacker.any? ? embed.dig(*stacker) : embed
end
end
end
diff --git a/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb
new file mode 100644
index 00000000000..447311b7029
--- /dev/null
+++ b/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb
@@ -0,0 +1,148 @@
+# 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::ProjectSqlCollectionRepresenter, 'rendering' do
+ include ::API::V3::Utilities::PathHelper
+
+ subject(:json) do
+ ::API::V3::Utilities::SqlRepresenterWalker
+ .new(scope,
+ embed: { 'elements' => {} },
+ select: select,
+ current_user: current_user,
+ self_path: 'some_path',
+ url_query: { offset: 1, pageSize: 5 })
+ .walk(described_class)
+ .to_json
+ end
+
+ let(:scope) do
+ Project
+ .where(id: project.id)
+ end
+
+ let(:project) do
+ create(:project)
+ end
+
+ let(:role) { create(:role) }
+
+ let(:select) do
+ { '*' => {}, 'elements' => { '*' => {} } }
+ end
+
+ current_user do
+ create(:user,
+ member_in_project: project,
+ member_through_role: role)
+ end
+
+ context 'when rendering everything' do
+ let(:expected) do
+ {
+ _type: "Collection",
+ pageSize: 5,
+ total: 1,
+ count: 1,
+ offset: 1,
+ _embedded: {
+ elements: [
+ {
+ id: project.id,
+ _type: "Project",
+ name: project.name,
+ _links: {
+ ancestors: [],
+ self: {
+ href: api_v3_paths.project(project.id),
+ title: project.name
+ }
+ }
+ }
+ ]
+ },
+ _links: {
+ self: {
+ # TODO: reflect selections
+ href: "some_path?offset=1&pageSize=5"
+ },
+ changeSize: {
+ href: "some_path?offset=1&pageSize=%7Bsize%7D",
+ templated: true
+ },
+ jumpTo: {
+ href: "some_path?offset=%7Boffset%7D&pageSize=5",
+ templated: true
+ }
+ }
+ }.to_json
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected)
+ end
+ end
+
+ context 'when rendering only collection attributes' do
+ let(:select) do
+ { '*' => {} }
+ end
+
+ let(:expected) do
+ {
+ _type: "Collection",
+ pageSize: 5,
+ total: 1,
+ count: 1,
+ offset: 1,
+ # TODO: remove empty embedded
+ _embedded: {},
+ _links: {
+ self: {
+ # TODO: reflect selections
+ href: "some_path?offset=1&pageSize=5"
+ },
+ changeSize: {
+ href: "some_path?offset=1&pageSize=%7Bsize%7D",
+ templated: true
+ },
+ jumpTo: {
+ href: "some_path?offset=%7Boffset%7D&pageSize=5",
+ templated: true
+ }
+ }
+ }.to_json
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected)
+ end
+ end
+end