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

minimal support for signaling on principals end point

ulfertsfda76ed75e9f
diff --git a/lib/api/decorators/sql/hal.rb b/lib/api/decorators/sql/hal.rb
index a049a7a85be..200ab270a9a 100644
--- a/lib/api/decorators/sql/hal.rb
+++ b/lib/api/decorators/sql/hal.rb
@@ -180,9 +180,7 @@ module API
def select_sql(select, walker_result)
<<~SELECT
json_strip_nulls(json_build_object(
- #{[properties_sql(select, walker_result),
- select_links(select, walker_result),
- select_embedded(select, walker_result)].compact_blank.join(', ')}
+ #{json_object_string(select, walker_result)}
))
SELECT
end
@@ -212,6 +210,21 @@ module API
SQL
end
+ protected
+
+ def json_object_string(select, walker_result)
+ [properties_sql(select, walker_result),
+ select_links(select, walker_result),
+ select_embedded(select, walker_result)]
+ .compact_blank
+ .join(', ')
+ end
+
+ # All properties and links that the client can correctly signal to have selected.
+ def valid_selects
+ links.keys + properties.keys + [:*]
+ end
+
private
def select_embedded(select, walker_result)
@@ -258,10 +271,9 @@ module API
end
def ensure_valid_selects(requested)
- supported = links.keys + properties.keys + [:*]
- invalid = requested - supported
+ invalid = requested - valid_selects
- raise API::Errors::InvalidSignal.new(invalid, supported, :select) if invalid.any?
+ raise API::Errors::InvalidSignal.new(invalid, valid_selects, :select) if invalid.any?
end
def namespaced_json_object(namespace)
diff --git a/lib/api/v3/principals/principal_collection_representer.rb b/lib/api/v3/principals/principal_collection_representer.rb
new file mode 100644
index 00000000000..466147bfc6b
--- /dev/null
+++ b/lib/api/v3/principals/principal_collection_representer.rb
@@ -0,0 +1,37 @@
+# OpenProject is an open source project management software.
+# Copyright (C) 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.
+
+#-- encoding: UTF-8
+
+# Exists to satisfy the expectations of the default end points.
+module API
+ module V3
+ module Principals
+ class PrincipalCollectionRepresenter < ::API::V3::Users::UserCollectionRepresenter
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/principals/principal_sql_collection_representer.rb b/lib/api/v3/principals/principal_sql_collection_representer.rb
new file mode 100644
index 00000000000..e7683b7bf6a
--- /dev/null
+++ b/lib/api/v3/principals/principal_sql_collection_representer.rb
@@ -0,0 +1,37 @@
+# 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.
+
+module API
+ module V3
+ module Principals
+ class PrincipalSqlCollectionRepresenter < API::Decorators::SqlCollectionRepresenter
+ self.embed_map = {
+ elements: PrincipalSqlRepresenter
+ }.with_indifferent_access
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/principals/principal_sql_representer.rb b/lib/api/v3/principals/principal_sql_representer.rb
new file mode 100644
index 00000000000..c44d19f3094
--- /dev/null
+++ b/lib/api/v3/principals/principal_sql_representer.rb
@@ -0,0 +1,70 @@
+# 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.
+
+module API
+ module V3
+ module Principals
+ # This representer is able to render all the concrete classes of Principal: User, Group and PlaceholderUser.
+ class PrincipalSqlRepresenter
+ include API::Decorators::Sql::Hal
+
+ class << self
+ def select_sql(select, walker_result)
+ <<~SELECT
+ json_strip_nulls(
+ CASE
+ WHEN type = 'Group' THEN json_build_object(#{group_select_sql(select, walker_result)})
+ WHEN type = 'PlaceholderUser' THEN json_build_object(#{placeholder_user_select_sql(select, walker_result)})
+ WHEN type = 'User' THEN json_build_object(#{user_select_sql(select, walker_result)})
+ END
+ )
+ SELECT
+ end
+
+ private
+
+ def group_select_sql(select, walker_result)
+ API::V3::Groups::GroupSqlRepresenter.json_object_string(select, walker_result)
+ end
+
+ def placeholder_user_select_sql(select, walker_result)
+ API::V3::PlaceholderUsers::PlaceholderUserSqlRepresenter.json_object_string(select, walker_result)
+ end
+
+ def user_select_sql(select, walker_result)
+ API::V3::Users::UserSqlRepresenter.json_object_string(select, walker_result)
+ end
+
+ def valid_selects
+ API::V3::Groups::GroupSqlRepresenter.valid_selects &
+ API::V3::PlaceholderUsers::PlaceholderUserSqlRepresenter.valid_selects &
+ API::V3::Users::UserSqlRepresenter.valid_selects
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/principals/principals_api.rb b/lib/api/v3/principals/principals_api.rb
index a37b40df1c6..ee735df8e01 100644
--- a/lib/api/v3/principals/principals_api.rb
+++ b/lib/api/v3/principals/principals_api.rb
@@ -31,10 +31,9 @@ module API
module Principals
class PrincipalsAPI < ::API::OpenProjectAPI
resource :principals do
- get &::API::V3::Utilities::Endpoints::Index
+ get &::API::V3::Utilities::Endpoints::SqlFallbackedIndex
.new(model: Principal,
- scope: -> { Principal.visible(current_user).includes(:preference) },
- render_representer: ::API::V3::Users::UserCollectionRepresenter)
+ scope: -> { Principal.visible(current_user).includes(:preference) })
.mount
end
end
diff --git a/spec/lib/api/v3/principals/principal_sql_representer_rendering_spec.rb b/spec/lib/api/v3/principals/principal_sql_representer_rendering_spec.rb
new file mode 100644
index 00000000000..a2d40ae1444
--- /dev/null
+++ b/spec/lib/api/v3/principals/principal_sql_representer_rendering_spec.rb
@@ -0,0 +1,141 @@
+# 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::Principals::PrincipalSqlRepresenter, 'rendering' do
+ include ::API::V3::Utilities::PathHelper
+
+ subject(:json) do
+ ::API::V3::Utilities::SqlRepresenterWalker
+ .new(scope,
+ current_user: current_user,
+ url_query: { select: select })
+ .walk(described_class)
+ .to_json
+ end
+
+ let(:scope) do
+ Principal
+ .where(id: rendered_principal.id)
+ end
+
+ let(:group) { create(:group) }
+ let(:placeholder_user) { create(:placeholder_user) }
+
+ let(:select) { { '*' => {} } }
+
+ current_user do
+ create(:user)
+ end
+
+ context 'when rendering all supported properties for a group' do
+ let(:rendered_principal) { group }
+
+ let(:expected) do
+ {
+ _type: "Group",
+ id: group.id,
+ name: group.name,
+ _links: {
+ self: {
+ href: api_v3_paths.group(group.id),
+ title: group.name
+ }
+ }
+ }
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
+ end
+
+ context 'when rendering all supported properties for a placeholder user' do
+ let(:rendered_principal) { placeholder_user }
+
+ let(:expected) do
+ {
+ _type: "PlaceholderUser",
+ id: placeholder_user.id,
+ name: placeholder_user.name,
+ _links: {
+ self: {
+ href: api_v3_paths.placeholder_user(placeholder_user.id),
+ title: placeholder_user.name
+ }
+ }
+ }
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
+ end
+
+ context 'when rendering all supported properties for a user' do
+ let(:rendered_principal) { current_user }
+
+ let(:expected) do
+ {
+ _type: "User",
+ id: current_user.id,
+ name: current_user.name,
+ firstname: current_user.firstname,
+ lastname: current_user.lastname,
+ _links: {
+ self: {
+ href: api_v3_paths.user(current_user.id),
+ title: current_user.name
+ }
+ }
+ }
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
+ end
+
+ context 'when rendering only the name property for a user' do
+ let(:rendered_principal) { current_user }
+ let(:select) { { 'name' => {} } }
+
+ let(:expected) do
+ {
+ name: current_user.name
+ }
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
+ end
+end
diff --git a/spec/requests/api/v3/principals/principals_resource_spec.rb b/spec/requests/api/v3/principals/principals_resource_spec.rb
index ca7ba5308a0..276d1b88658 100644
--- a/spec/requests/api/v3/principals/principals_resource_spec.rb
+++ b/spec/requests/api/v3/principals/principals_resource_spec.rb
@@ -37,10 +37,11 @@ describe 'API v3 Principals resource', type: :request do
subject(:response) { last_response }
let(:path) do
- api_v3_paths.path_for :principals, filters: filter, sort_by: order
+ api_v3_paths.path_for :principals, filters: filter, sort_by: order, select: select
end
let(:order) { { name: :desc } }
let(:filter) { nil }
+ let(:select) { nil }
let(:project) { create(:project) }
let(:other_project) { create(:project) }
let(:non_member_project) { create(:project) }
@@ -166,5 +167,71 @@ describe 'API v3 Principals resource', type: :request do
let(:elements) { [current_user] }
end
end
+
+ context 'when signaling' do
+ let(:select) { 'total,count,elements/*' }
+
+ let(:expected) do
+ {
+ total: 4,
+ count: 4,
+ _embedded: {
+ elements: [
+ {
+ _type: 'PlaceholderUser',
+ id: placeholder_user.id,
+ name: placeholder_user.name,
+ _links: {
+ self: {
+ href: api_v3_paths.placeholder_user(placeholder_user.id),
+ title: placeholder_user.name
+ }
+ }
+ },
+ {
+ _type: 'Group',
+ id: group.id,
+ name: group.name,
+ _links: {
+ self: {
+ href: api_v3_paths.group(group.id),
+ title: group.name
+ }
+ }
+ },
+ {
+ _type: "User",
+ id: other_user.id,
+ name: other_user.name,
+ _links: {
+ self: {
+ href: api_v3_paths.user(other_user.id),
+ title: other_user.name
+ }
+ }
+ },
+ {
+ _type: "User",
+ id: user.id,
+ name: user.name,
+ firstname: user.firstname,
+ lastname: user.lastname,
+ _links: {
+ self: {
+ href: api_v3_paths.user(user.id),
+ title: user.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
+ end
end
end