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
signaling on capabilities api
ulferts95fc2ba7c4b1
diff --git a/lib/api/decorators/sql/hal.rb b/lib/api/decorators/sql/hal.rbindex 00d2c0f95e2..e2275181ad9 100644--- a/lib/api/decorators/sql/hal.rb+++ b/lib/api/decorators/sql/hal.rb@@ -93,13 +93,9 @@ module APIdef links_selects(select, walker_result)selected_links(select).map do |name, link|- path_name = link[:path] ? link[:path][:api] : nametitle = 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 titlelink_attributes += ["'title'", title]@@ -110,7 +106,7 @@ module APIendif link[:render_if]- <<-SQL+ <<-SQL.squish'#{name}',CASE WHEN #{link[:render_if].call(walker_result)} THENjson_build_object(#{link_attributes.join(', ')})@@ -119,7 +115,7 @@ module APIENDSQLelse- <<-SQL+ <<-SQL.squish'#{name}', json_build_object(#{link_attributes.join(', ')})SQLend@@ -143,7 +139,7 @@ module APIlink[:column]end- <<-SQL+ <<-SQL.squish'#{name}', #{representation}SQLend@@ -155,7 +151,7 @@ module APIjson_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(', ')}))SELECTend@@ -166,7 +162,7 @@ module APIdef to_sql(walker_result)ctes = walker_result.ctes.map do |key, sql|- <<~SQL+ <<~SQL.squish#{key} AS (#{sql})@@ -175,7 +171,7 @@ module APIctes_sql = ctes.any? ? "WITH #{ctes.join(', ')}" : ""- <<~SQL+ <<~SQL.squish#{ctes_sql}SELECT@@ -188,23 +184,15 @@ module APIprivatedef 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)endenddef 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)+ endenddef select_from(walker_result)@@ -244,6 +232,25 @@ module APIraise 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})"+ endendendenddiff --git a/lib/api/decorators/sql_collection_representer.rb b/lib/api/decorators/sql_collection_representer.rbindex 4a935b27837..859d61ed0e0 100644--- a/lib/api/decorators/sql_collection_representer.rb+++ b/lib/api/decorators/sql_collection_representer.rb@@ -80,27 +80,27 @@ module APIlink :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: truelink :changeSize,href: ->(walker_result) { "'#{full_self_path(walker_result, pageSize: '{size}')}'" },- title: -> { nil },+ title: -> {},templated: truelink :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) dodiff --git a/lib/api/v3/capabilities/capability_sql_representer.rb b/lib/api/v3/capabilities/capability_sql_representer.rbindex 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 APIproperty :id,representation: ->(*) {- <<~SQL+ <<~SQL.squishCASEWHEN context_id IS NULL THEN action || '/g-' || principal_idELSE action || '/p' || context_id || '-' || principal_id@@ -50,22 +50,22 @@ module APIlink :self,path: { api: :capability, params: %w(action) },column: -> {- <<~SQL+ <<~SQL.squishCASEWHEN context_id IS NULL THEN action || '/g-' || principal_idELSE action || '/p' || context_id || '-' || principal_idENDSQL},- title: -> { nil }+ title: -> {}link :action,path: { api: :action, params: %w(action) },- title: -> { nil }+ title: -> {}link :context,href: ->(*) {- <<~SQL+ <<~SQL.squishCASEWHEN 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 APISQL},title: ->(*) {- <<~SQL+ <<~SQL.squishCASEWHEN context_id IS NULL THEN '#{I18n.t('activerecord.errors.models.capability.context.global')}'ELSE context_name@@ -86,7 +86,7 @@ module APIlink :principal,href: ->(*) {- <<~SQL+ <<~SQL.squishCASE principal_typeWHEN '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.squishCASE principal_typeWHEN 'Group' THEN lastnameWHEN 'PlaceholderUser' THEN lastnamediff --git a/lib/api/v3/utilities/endpoints/sql_index.rb b/lib/api/v3/utilities/endpoints/sql_index.rbindex 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 APIdef deduce_render_representer"::API::V3::#{deduce_api_namespace}::#{api_name}SqlCollectionRepresenter".constantizeend++ 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]) : {} }+ endendendenddiff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rbindex 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_blankif 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.rbindex 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 doendend- context 'invalid filter' do+ context 'with an invalid filter' dolet(:filters) do[{ 'bogus' => {'operator' => '=',@@ -320,6 +320,39 @@ describe 'API v3 capabilities resource', type: :request, content_type: :json doendend+ 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' docurrent_user docreate(:user)@@ -382,7 +415,7 @@ describe 'API v3 capabilities resource', type: :request, content_type: :json doit 'returns 200 OK' doexpect(subject.status)- .to eql(200)+ .to be(200)endit 'returns the capability' do