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

stub for signaling on work package resource

ulfertsa80767f516a5
diff --git a/app/services/api/v3/work_package_collection_from_query_service.rb b/app/services/api/v3/work_package_collection_from_query_service.rb
index 1226013791e..0667fb23951 100644
--- a/app/services/api/v3/work_package_collection_from_query_service.rb
+++ b/app/services/api/v3/work_package_collection_from_query_service.rb
@@ -30,7 +30,7 @@ module API
module V3
class WorkPackageCollectionFromQueryService
include Utilities::PathHelper
- include ::API::Utilities::PageSizeHelper
+ include ::API::Utilities::UrlPropsParsingHelper
def initialize(query, user, scope: nil)
self.query = query
@@ -88,6 +88,8 @@ module API
params[:offset] = to_i_or_nil(params[:offset])
params[:pageSize] = pageSizeParam(params)
end
+
+ params[:select] = nested_from_csv(provided_params['select'])
end
end
@@ -147,18 +149,27 @@ module API
def collection_representer(work_packages, params:, project:, groups:, sums:)
resulting_params = calculate_resulting_params(params)
- ::API::V3::WorkPackages::WorkPackageCollectionRepresenter.new(
- work_packages,
- self_link: self_link(project),
- project: project,
- query: resulting_params,
- page: resulting_params[:offset],
- per_page: resulting_params[:pageSize],
- groups: groups,
- total_sums: sums,
- embed_schemas: true,
- current_user: current_user
- )
+ if resulting_params[:select]
+ ::API::V3::Utilities::SqlRepresenterWalker
+ .new(work_packages,
+ current_user: current_user,
+ self_path: self_link(project),
+ url_query: resulting_params)
+ .walk(::API::V3::WorkPackages::WorkPackageSqlCollectionRepresenter)
+ else
+ ::API::V3::WorkPackages::WorkPackageCollectionRepresenter.new(
+ work_packages,
+ self_link: self_link(project),
+ project: project,
+ query: resulting_params,
+ page: resulting_params[:offset],
+ per_page: resulting_params[:pageSize],
+ groups: groups,
+ total_sums: sums,
+ embed_schemas: true,
+ current_user: current_user
+ )
+ end
end
def to_i_or_nil(value)
diff --git a/docs/api/apiv3/paths/project_work_packages.yml b/docs/api/apiv3/paths/project_work_packages.yml
index c50067e8fdf..dd22bd7d78b 100644
--- a/docs/api/apiv3/paths/project_work_packages.yml
+++ b/docs/api/apiv3/paths/project_work_packages.yml
@@ -60,6 +60,14 @@ get:
schema:
default: 'false'
type: boolean
+ - description: |-
+ Comma separated list of properties to include.
+ example: 'total,elements/subject,elements/id,self'
+ in: query
+ name: select
+ required: false
+ schema:
+ type: string
responses:
'200':
content:
diff --git a/docs/api/apiv3/paths/work_packages.yml b/docs/api/apiv3/paths/work_packages.yml
index 2317662641d..be6b312366c 100644
--- a/docs/api/apiv3/paths/work_packages.yml
+++ b/docs/api/apiv3/paths/work_packages.yml
@@ -111,6 +111,14 @@ get:
schema:
default: 'false'
type: boolean
+ - description: |-
+ Comma separated list of properties to include.
+ example: 'total,elements/subject,elements/id,self'
+ in: query
+ name: select
+ required: false
+ schema:
+ type: string
responses:
'200':
content:
diff --git a/lib/api/decorators/offset_paginated_collection.rb b/lib/api/decorators/offset_paginated_collection.rb
index 124c5e7a8ca..6349c765833 100644
--- a/lib/api/decorators/offset_paginated_collection.rb
+++ b/lib/api/decorators/offset_paginated_collection.rb
@@ -31,7 +31,7 @@
module API
module Decorators
class OffsetPaginatedCollection < ::API::Decorators::Collection
- include ::API::Utilities::PageSizeHelper
+ include ::API::Utilities::UrlPropsParsingHelper
def self.per_page_default(relation)
relation.base_class.per_page
diff --git a/lib/api/decorators/sql_collection_representer.rb b/lib/api/decorators/sql_collection_representer.rb
index 87dc6cf944f..11a958d8ddb 100644
--- a/lib/api/decorators/sql_collection_representer.rb
+++ b/lib/api/decorators/sql_collection_representer.rb
@@ -35,7 +35,10 @@ module API
def ctes(walker_result)
{
projection: walker_result.projection_scope.limit(sql_limit(walker_result)).offset(sql_offset(walker_result)).to_sql,
- total: walker_result.filter_scope.reselect('COUNT(*)').except(:order).to_sql
+ # Currently there does not appear to be a way to correctly transform the filter_scope into sql
+ # (e.g. by calling #to_sql) so that the extra call to the database is avoided. The main problem
+ # is that LEFT JOINS added via includes are not part of the SQL generated by #to_sql.
+ total: "SELECT #{walker_result.filter_scope.count}"
}
end
diff --git a/lib/api/utilities/endpoints/index.rb b/lib/api/utilities/endpoints/index.rb
index 9b188c37cf6..33851ecbd97 100644
--- a/lib/api/utilities/endpoints/index.rb
+++ b/lib/api/utilities/endpoints/index.rb
@@ -30,7 +30,7 @@ module API
module Utilities
module Endpoints
class Index
- include ::API::Utilities::PageSizeHelper
+ include ::API::Utilities::UrlPropsParsingHelper
def initialize(model:,
api_name: model.name.demodulize,
diff --git a/lib/api/utilities/page_size_helper.rb b/lib/api/utilities/url_props_parsing_helper.rb
similarity index 74%
rename from lib/api/utilities/page_size_helper.rb
rename to lib/api/utilities/url_props_parsing_helper.rb
index eeef7bd13d9..c1c0d65615c 100644
--- a/lib/api/utilities/page_size_helper.rb
+++ b/lib/api/utilities/url_props_parsing_helper.rb
@@ -30,7 +30,7 @@
module API
module Utilities
- module PageSizeHelper
+ module UrlPropsParsingHelper
##
# Determine set page_size from string
def resolve_page_size(string)
@@ -68,6 +68,29 @@ module API
def to_i_or_nil(string)
string ? string.to_i : nil
end
+
+ # Parses a comma separated list of values and turns it into
+ # a nested hash. e.g.:
+ # = "a,b/c/d,e,b/f"
+ # is turned into:
+ # = { "a" => {}, "e" => {}, "b" => { "c" => { "d" => {} }, "f" => {} } }
+ # The order of the values does not matter.
+ # It also accepts an array of individual strings, e.g.:
+ # = ["a","b/c/d","e","b/f"]
+ def nested_from_csv(value)
+ return unless value
+
+ value
+ .delete_prefix('[')
+ .delete_suffix(']')
+ .split(',')
+ .map { |path| nested_hash(path.strip.tr("\"'", '').split('/')) }
+ .inject({}) { |hash, nested| hash.deep_merge(nested) }
+ end
+
+ def nested_hash(path)
+ { path[0] => path.length > 1 ? nested_hash(path[1..]) : {} }
+ end
end
end
end
diff --git a/lib/api/v3/memberships/memberships_api.rb b/lib/api/v3/memberships/memberships_api.rb
index ff678a7da42..1eb5129c008 100644
--- a/lib/api/v3/memberships/memberships_api.rb
+++ b/lib/api/v3/memberships/memberships_api.rb
@@ -30,7 +30,7 @@ module API
module V3
module Memberships
class MembershipsAPI < ::API::OpenProjectAPI
- helpers ::API::Utilities::PageSizeHelper
+ helpers ::API::Utilities::UrlPropsParsingHelper
resources :memberships do
get &::API::V3::Utilities::Endpoints::Index
diff --git a/lib/api/v3/utilities/endpoints/sql_index.rb b/lib/api/v3/utilities/endpoints/sql_index.rb
index 0486c47e9b3..80c471c75b5 100644
--- a/lib/api/v3/utilities/endpoints/sql_index.rb
+++ b/lib/api/v3/utilities/endpoints/sql_index.rb
@@ -54,26 +54,9 @@ module API
def calculate_resulting_params(query, provided_params)
super.tap do |params|
- params[:select] = nested_from_params(provided_params, 'select') || { '*' => {}, 'elements' => { '*' => {} } }
+ params[:select] = nested_from_csv(provided_params['select']) || { '*' => {}, 'elements' => { '*' => {} } }
end
end
-
- def nested_from_params(params, key)
- key_params = params[key]
-
- return unless key_params
-
- key_params
- .delete_prefix('[')
- .delete_suffix(']')
- .split(',')
- .map { |path| nested_hash(path.strip.tr("\"'", '').split('/')) }
- .inject({}) { |hash, nested| hash.deep_merge(nested) }
- end
-
- def nested_hash(path)
- { path[0] => path.length > 1 ? nested_hash(path[1..]) : {} }
- end
end
end
end
diff --git a/lib/api/v3/utilities/sql_representer_walker.rb b/lib/api/v3/utilities/sql_representer_walker.rb
index b30f4692d67..308df32e713 100644
--- a/lib/api/v3/utilities/sql_representer_walker.rb
+++ b/lib/api/v3/utilities/sql_representer_walker.rb
@@ -32,7 +32,7 @@ module API
module V3
module Utilities
class SqlRepresenterWalker
- include API::Utilities::PageSizeHelper
+ include API::Utilities::UrlPropsParsingHelper
def initialize(scope,
current_user:,
diff --git a/lib/api/v3/work_packages/watchers_api.rb b/lib/api/v3/work_packages/watchers_api.rb
index 2fb4f359b8c..52d17d04622 100644
--- a/lib/api/v3/work_packages/watchers_api.rb
+++ b/lib/api/v3/work_packages/watchers_api.rb
@@ -32,7 +32,7 @@ module API
module V3
module WorkPackages
class WatchersAPI < ::API::OpenProjectAPI
- helpers ::API::Utilities::PageSizeHelper
+ helpers ::API::Utilities::UrlPropsParsingHelper
get '/available_watchers' do
authorize(:add_work_package_watchers, context: @work_package.project)
diff --git a/lib/api/v3/work_packages/work_package_sql_collection_representer.rb b/lib/api/v3/work_packages/work_package_sql_collection_representer.rb
new file mode 100644
index 00000000000..d19a5301ad7
--- /dev/null
+++ b/lib/api/v3/work_packages/work_package_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 WorkPackages
+ class WorkPackageSqlCollectionRepresenter < API::Decorators::SqlCollectionRepresenter
+ self.embed_map = {
+ elements: WorkPackageSqlRepresenter
+ }.with_indifferent_access
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/work_packages/work_package_sql_representer.rb b/lib/api/v3/work_packages/work_package_sql_representer.rb
new file mode 100644
index 00000000000..878449ac402
--- /dev/null
+++ b/lib/api/v3/work_packages/work_package_sql_representer.rb
@@ -0,0 +1,47 @@
+# 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 WorkPackages
+ class WorkPackageSqlRepresenter
+ include API::Decorators::Sql::Hal
+
+ link :self,
+ path: { api: :work_package, params: %w(id) },
+ column: -> { :id },
+ title: -> { 'subject' }
+
+ property :_type,
+ representation: ->(*) { "'WorkPackage'" }
+
+ property :id
+
+ property :subject
+ end
+ end
+ end
+end
diff --git a/modules/costs/lib/api/v3/time_entries/time_entries_api.rb b/modules/costs/lib/api/v3/time_entries/time_entries_api.rb
index eb90c6d7a42..2a413eaea0f 100644
--- a/modules/costs/lib/api/v3/time_entries/time_entries_api.rb
+++ b/modules/costs/lib/api/v3/time_entries/time_entries_api.rb
@@ -30,7 +30,7 @@ module API
module V3
module TimeEntries
class TimeEntriesAPI < ::API::OpenProjectAPI
- helpers ::API::Utilities::PageSizeHelper
+ helpers ::API::Utilities::UrlPropsParsingHelper
resources :time_entries do
get &::API::V3::Utilities::Endpoints::Index.new(model: TimeEntry).mount
diff --git a/modules/documents/lib/api/v3/documents/documents_api.rb b/modules/documents/lib/api/v3/documents/documents_api.rb
index abfe0bd0f07..64273c8f786 100644
--- a/modules/documents/lib/api/v3/documents/documents_api.rb
+++ b/modules/documents/lib/api/v3/documents/documents_api.rb
@@ -30,7 +30,7 @@ module API
module V3
module Documents
class DocumentsAPI < ::API::OpenProjectAPI
- helpers ::API::Utilities::PageSizeHelper
+ helpers ::API::Utilities::UrlPropsParsingHelper
resources :documents do
get do
diff --git a/modules/grids/app/controllers/api/v3/grids/grids_api.rb b/modules/grids/app/controllers/api/v3/grids/grids_api.rb
index 82cfaf78d58..7422f749ed5 100644
--- a/modules/grids/app/controllers/api/v3/grids/grids_api.rb
+++ b/modules/grids/app/controllers/api/v3/grids/grids_api.rb
@@ -32,7 +32,7 @@ module API
class GridsAPI < ::API::OpenProjectAPI
resources :grids do
helpers do
- include API::Utilities::PageSizeHelper
+ include API::Utilities::UrlPropsParsingHelper
end
get do
diff --git a/spec/lib/api/utilities/page_size_helper_spec.rb b/spec/lib/api/utilities/url_props_parsing_helper_spec.rb
similarity index 94%
rename from spec/lib/api/utilities/page_size_helper_spec.rb
rename to spec/lib/api/utilities/url_props_parsing_helper_spec.rb
index 322a084f78e..1f22ad4a867 100644
--- a/spec/lib/api/utilities/page_size_helper_spec.rb
+++ b/spec/lib/api/utilities/url_props_parsing_helper_spec.rb
@@ -28,10 +28,10 @@
require 'spec_helper'
-describe ::API::Utilities::PageSizeHelper do
+describe ::API::Utilities::UrlPropsParsingHelper do
let(:clazz) do
Class.new do
- include ::API::Utilities::PageSizeHelper
+ include ::API::Utilities::UrlPropsParsingHelper
end
end
let(:subject) { clazz.new }
diff --git a/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb b/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb
new file mode 100644
index 00000000000..015aedf830b
--- /dev/null
+++ b/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb
@@ -0,0 +1,74 @@
+# 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::WorkPackages::WorkPackageSqlRepresenter, '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
+ WorkPackage
+ .where(id: rendered_work_package.id)
+ end
+
+ let(:rendered_work_package) { create(:work_package) }
+
+ let(:select) { { '*' => {} } }
+
+ current_user do
+ create(:user)
+ end
+
+ context 'when rendering all supported properties' do
+ let(:expected) do
+ {
+ _type: "WorkPackage",
+ id: rendered_work_package.id,
+ subject: rendered_work_package.subject,
+ _links: {
+ self: {
+ href: api_v3_paths.work_package(rendered_work_package.id),
+ title: rendered_work_package.subject
+ }
+ }
+ }
+ end
+
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
+ end
+end