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

signaling on capabilities api

ulferts95fc2ba7c4b1
diff --git a/lib/api/decorators/sql/hal.rb b/lib/api/decorators/sql/hal.rb
index 00d2c0f95e2..e2275181ad9 100644
--- a/lib/api/decorators/sql/hal.rb
+++ b/lib/api/decorators/sql/hal.rb
@@ -93,13 +93,9 @@ module API
def links_selects(select, walker_result)
selected_links(select)
.map do |name, link|
- path_name = link[:path] ? link[:path][:api] : name
title = link[:title] ? link[:title].call : "#{name}.name"
- column = link[:column] ? link[:column].call : name
- href = link[:href] ? link[:href].call(walker_result) : "format('#{api_v3_paths.send(path_name, '%s')}', #{column})"
-
- link_attributes = ["'href'", href]
+ link_attributes = ["'href'", link_href(link, name, walker_result)]
if title
link_attributes += ["'title'", title]
@@ -110,7 +106,7 @@ module API
end
if link[:render_if]
- <<-SQL
+ <<-SQL.squish
'#{name}',
CASE WHEN #{link[:render_if].call(walker_result)} THEN
json_build_object(#{link_attributes.join(', ')})
@@ -119,7 +115,7 @@ module API
END
SQL
else
- <<-SQL
+ <<-SQL.squish
'#{name}', json_build_object(#{link_attributes.join(', ')})
SQL
end
@@ -143,7 +139,7 @@ module API
link[:column]
end
- <<-SQL
+ <<-SQL.squish
'#{name}', #{representation}
SQL
end
@@ -155,7 +151,7 @@ module API
json_strip_nulls(json_build_object(
#{[properties_sql(select, walker_result),
select_links(select, walker_result),
- select_embedded(select, walker_result)].compact.join(', ')}
+ select_embedded(select, walker_result)].compact_blank.join(', ')}
))
SELECT
end
@@ -166,7 +162,7 @@ module API
def to_sql(walker_result)
ctes = walker_result.ctes.map do |key, sql|
- <<~SQL
+ <<~SQL.squish
#{key} AS (
#{sql}
)
@@ -175,7 +171,7 @@ module API
ctes_sql = ctes.any? ? "WITH #{ctes.join(', ')}" : ""
- <<~SQL
+ <<~SQL.squish
#{ctes_sql}
SELECT
@@ -188,23 +184,15 @@ module API
private
def select_embedded(select, walker_result)
- embedded = embedded_selects(select, walker_result)
-
- if embedded.present?
- <<~SQL
- '_embedded', json_strip_nulls(json_build_object(
- #{embedded_selects(select, walker_result)}
- ))
- SQL
+ namespaced_json_object('_embedded') do
+ embedded_selects(select, walker_result)
end
end
def select_links(select, walker_result)
- <<~SELECT
- '_links', json_strip_nulls(json_build_object(
- #{links_selects(select, walker_result)}
- ))
- SELECT
+ namespaced_json_object('_links') do
+ links_selects(select, walker_result)
+ end
end
def select_from(walker_result)
@@ -244,6 +232,25 @@ module API
raise API::Errors::InvalidSignal.new(invalid, supported, :select) if invalid.any?
end
+
+ def namespaced_json_object(namespace)
+ json_object = yield
+
+ return if json_object.blank?
+
+ <<~SELECT
+ '#{namespace}', json_strip_nulls(json_build_object(
+ #{json_object}
+ ))
+ SELECT
+ end
+
+ def link_href(link, name, walker_result)
+ path_name = link[:path] ? link[:path][:api] : name
+ column = link[:column] ? link[:column].call : name
+
+ link[:href] ? link[:href].call(walker_result) : "format('#{api_v3_paths.send(path_name, '%s')}', #{column})"
+ end
end
end
end
diff --git a/lib/api/decorators/sql_collection_representer.rb b/lib/api/decorators/sql_collection_representer.rb
index 4a935b27837..859d61ed0e0 100644
--- a/lib/api/decorators/sql_collection_representer.rb
+++ b/lib/api/decorators/sql_collection_representer.rb
@@ -80,27 +80,27 @@ module API
link :self,
href: ->(walker_result) { "'#{full_self_path(walker_result)}'" },
- title: -> { nil }
+ title: -> {}
link :jumpTo,
href: ->(walker_result) { "'#{full_self_path(walker_result, offset: '{offset}')}'" },
- title: -> { nil },
+ title: -> {},
templated: true
link :changeSize,
href: ->(walker_result) { "'#{full_self_path(walker_result, pageSize: '{size}')}'" },
- title: -> { nil },
+ title: -> {},
templated: true
link :previousByOffset,
href: ->(walker_result) { "'#{full_self_path(walker_result, offset: walker_result.offset - 1)}'" },
render_if: ->(walker_result) { walker_result.offset > 1 },
- title: -> { nil }
+ title: -> {}
link :nextByOffset,
href: ->(walker_result) { "'#{full_self_path(walker_result, offset: walker_result.offset + 1)}'" },
render_if: ->(walker_result) { "#{walker_result.offset * walker_result.page_size} < (SELECT * FROM total)" },
- title: -> { nil }
+ title: -> {}
embedded :elements,
representation: ->(walker_result) do
diff --git a/lib/api/v3/capabilities/capability_sql_representer.rb b/lib/api/v3/capabilities/capability_sql_representer.rb
index f090927e65a..3a5fb094256 100644
--- a/lib/api/v3/capabilities/capability_sql_representer.rb
+++ b/lib/api/v3/capabilities/capability_sql_representer.rb
@@ -39,7 +39,7 @@ module API
property :id,
representation: ->(*) {
- <<~SQL
+ <<~SQL.squish
CASE
WHEN context_id IS NULL THEN action || '/g-' || principal_id
ELSE action || '/p' || context_id || '-' || principal_id
@@ -50,22 +50,22 @@ module API
link :self,
path: { api: :capability, params: %w(action) },
column: -> {
- <<~SQL
+ <<~SQL.squish
CASE
WHEN context_id IS NULL THEN action || '/g-' || principal_id
ELSE action || '/p' || context_id || '-' || principal_id
END
SQL
},
- title: -> { nil }
+ title: -> {}
link :action,
path: { api: :action, params: %w(action) },
- title: -> { nil }
+ title: -> {}
link :context,
href: ->(*) {
- <<~SQL
+ <<~SQL.squish
CASE
WHEN context_id IS NULL THEN '#{api_v3_paths.capabilities_contexts_global}'
ELSE format('#{api_v3_paths.project('%s')}', context_id)
@@ -73,7 +73,7 @@ module API
SQL
},
title: ->(*) {
- <<~SQL
+ <<~SQL.squish
CASE
WHEN context_id IS NULL THEN '#{I18n.t('activerecord.errors.models.capability.context.global')}'
ELSE context_name
@@ -86,7 +86,7 @@ module API
link :principal,
href: ->(*) {
- <<~SQL
+ <<~SQL.squish
CASE principal_type
WHEN 'Group' THEN format('#{api_v3_paths.group('%s')}', principal_id)
WHEN 'PlaceholderUser' THEN format('#{api_v3_paths.placeholder_user('%s')}', principal_id)
@@ -101,7 +101,7 @@ module API
" || ' ' || "
end
- <<~SQL
+ <<~SQL.squish
CASE principal_type
WHEN 'Group' THEN lastname
WHEN 'PlaceholderUser' THEN lastname
diff --git a/lib/api/v3/utilities/endpoints/sql_index.rb b/lib/api/v3/utilities/endpoints/sql_index.rb
index dc0291da4af..8bf1b2f06d1 100644
--- a/lib/api/v3/utilities/endpoints/sql_index.rb
+++ b/lib/api/v3/utilities/endpoints/sql_index.rb
@@ -39,7 +39,7 @@ module API
::API::V3::Utilities::SqlRepresenterWalker
.new(results,
embed: { 'elements' => {} },
- select: { '*' => {}, 'elements' => { '*' => {} } },
+ select: resulting_params[:select],
current_user: User.current,
self_path: self_path,
url_query: resulting_params)
@@ -53,6 +53,27 @@ module API
def deduce_render_representer
"::API::V3::#{deduce_api_namespace}::#{api_name}SqlCollectionRepresenter".constantize
end
+
+ def calculate_resulting_params(query, provided_params)
+ super.tap do |params|
+ params[:select] = nested_from_params(provided_params, 'select') || { '*' => {}, 'elements' => { '*' => {} } }
+ end
+ end
+
+ def nested_from_params(params, key)
+ key_params = params[key]
+
+ return unless key_params
+
+ key_params
+ .split(',')
+ .map { |path| nested_hash(path.split('/')) }
+ .inject({}) { |hash, nested| hash.deep_merge(nested) }
+ end
+
+ def nested_hash(path)
+ { path[0] => path.length > 1 ? nested_hash(path[1..-1]) : {} }
+ end
end
end
end
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index 2be4a485663..66824436112 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -497,14 +497,15 @@ module API
"#{project(project_id)}/work_packages"
end
- def self.path_for(path, filters: nil, sort_by: nil, group_by: nil, page_size: nil, offset: nil)
+ def self.path_for(path, filters: nil, sort_by: nil, group_by: nil, page_size: nil, offset: nil, select: nil)
query_params = {
filters: filters&.to_json,
sortBy: sort_by&.to_json,
groupBy: group_by,
pageSize: page_size,
- offset: offset
- }.reject { |_, v| v.blank? }
+ offset: offset,
+ select: select
+ }.compact_blank
if query_params.any?
"#{send(path)}?#{query_params.to_query}"
diff --git a/spec/requests/api/v3/capability_resource_spec.rb b/spec/requests/api/v3/capability_resource_spec.rb
index dc960051d2d..be867888a75 100644
--- a/spec/requests/api/v3/capability_resource_spec.rb
+++ b/spec/requests/api/v3/capability_resource_spec.rb
@@ -244,7 +244,7 @@ describe 'API v3 capabilities resource', type: :request, content_type: :json do
end
end
- context 'invalid filter' do
+ context 'with an invalid filter' do
let(:filters) do
[{ 'bogus' => {
'operator' => '=',
@@ -320,6 +320,39 @@ describe 'API v3 capabilities resource', type: :request, content_type: :json do
end
end
+ context 'when signaling to only include a subset of properties' do
+ let(:current_user_permissions) { %i[manage_members] }
+ let(:path) { api_v3_paths.path_for(:capabilities, filters: filters, sort_by: [%i(id asc)], select: 'elements/id') }
+
+ let(:filters) do
+ [{ 'principalId' => {
+ 'operator' => '=',
+ 'values' => [current_user.id.to_s]
+ } }]
+ end
+
+ it 'contains only the filtered capabilities in the response' do
+ expected = {
+ _embedded: {
+ elements: [
+ {
+ id: "memberships/create/p#{project.id}-#{current_user.id}"
+ },
+ {
+ id: "memberships/destroy/p#{project.id}-#{current_user.id}"
+ },
+ {
+ id: "memberships/update/p#{project.id}-#{current_user.id}"
+ }
+ ]
+ }
+ }
+
+ expect(subject.body)
+ .to be_json_eql(expected.to_json)
+ end
+ end
+
context 'without permissions' do
current_user do
create(:user)
@@ -382,7 +415,7 @@ describe 'API v3 capabilities resource', type: :request, content_type: :json do
it 'returns 200 OK' do
expect(subject.status)
- .to eql(200)
+ .to be(200)
end
it 'returns the capability' do