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
[#59391] updated API specs for new pagination in file links
Eric Schuberte86d5bd25140
diff --git a/docs/api/apiv3/components/schemas/file_link_collection_read_model.yml b/docs/api/apiv3/components/schemas/file_link_collection_read_model.ymlindex 6d59d113724..4d0693eea18 100644--- a/docs/api/apiv3/components/schemas/file_link_collection_read_model.yml+++ b/docs/api/apiv3/components/schemas/file_link_collection_read_model.yml@@ -1,28 +1,21 @@# Schema: FileLinkCollectionReadModel---allOf:- - $ref: './collection_model.yml'+ - $ref: './paginated_collection_model.yml'- type: object- required:- - _links- - _embeddedproperties:_links:type: object- required:- - selfproperties:self:allOf:- - $ref: "./link.yml"+ - $ref: './link.yml'- description: |-This file links collection**Resource**: FileLinkCollectionReadModel_embedded:type: object- required:- - elementsproperties:elements:type: array@@ -33,9 +26,17 @@ example:_type: Collectiontotal: 2count: 2+ pageSize: 30+ offset: 1_links:self:href: '/api/v3/work_packages/42/file_links'+ jumpTo:+ href: '/api/v3/work_packages/42/file_links?offset=%7Boffset%7D&pageSize=30'+ templated: true+ changeSize:+ href: '/api/v3/work_packages/42/file_links?offset=1&pageSize=%7Bsize%7D'+ templated: true_embedded:elements:- id: 1337diff --git a/docs/api/apiv3/components/schemas/file_link_read_model.yml b/docs/api/apiv3/components/schemas/file_link_read_model.ymlindex bcf76660873..8c912c5a444 100644--- a/docs/api/apiv3/components/schemas/file_link_read_model.yml+++ b/docs/api/apiv3/components/schemas/file_link_read_model.yml@@ -1,11 +1,6 @@# Schema: FileLinkReadModel---type: object-required:- - id- - _type- - originData- - _linksproperties:id:type: integer@@ -36,17 +31,6 @@ properties:$ref: './work_package_model.yml'_links:type: object- required:- - self- - storage- - container- - creator- - permission- - originOpen- - staticOriginOpen- - originOpenLocation- - staticOriginOpenLocation- - staticOriginDownloadproperties:self:allOf:@@ -166,24 +150,8 @@ example:container:_hint: Work package resource shortened for brevity_type: WorkPackage- _links:- self:- href: "/api/v3/work_packages/1528"- title: Develop API- schema:- href: "/api/v3/work_packages/schemas/11-2"id: 1528subject: Develop API- description:- format: markdown- raw: Develop super cool OpenProject API.- html: "<p>Develop super cool OpenProject API.</p>"- scheduleManually: false- readonly: false- startDate:- dueDate:- createdAt: '2014-08-29T12:40:53.860Z'- updatedAt: '2014-08-29T12:44:41.036Z'_links:self:href: /api/v3/work_package/17/file_links/1337@@ -199,8 +167,8 @@ example:delete:href: /api/v3/work_package/17/file_links/1337status:- href: urn:openproject-org:api:v3:file-links:permission:View- title: View+ href: urn:openproject-org:api:v3:file-links:permission:ViewAllowed+ title: View allowedoriginOpen:href: https://nextcloud.deathstar.rocks/index.php/f/5503?openfile=1staticOriginOpen:diff --git a/modules/storages/lib/api/v3/file_links/create_endpoint.rb b/modules/storages/lib/api/v3/file_links/create_endpoint.rbindex 4d40fd76e8f..af545e226cc 100644--- a/modules/storages/lib/api/v3/file_links/create_endpoint.rb+++ b/modules/storages/lib/api/v3/file_links/create_endpoint.rb@@ -28,59 +28,56 @@# See COPYRIGHT and LICENSE files for more details.#++-# Handles /api/v3/work_packages/:work_package_id/file_links as defined-# in modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb-#-# Multiple classes are involved during its lifecycle:-# - Storages::Peripherals::ParseCreateParamsService-# - API::V3::FileLinks::FileLinkCollectionRepresenter-# - Storages::FileLinks::CreateService-#-# These classes are either deduced from the model class, or given as parameter-# on class instantiation.-class API::V3::FileLinks::CreateEndpoint < API::Utilities::Endpoints::Create- include ::API::V3::Utilities::Endpoints::V3Deductions- include ::API::V3::Utilities::Endpoints::V3PresentSingle+module API+ module V3+ module FileLinks+ class CreateEndpoint < API::Utilities::Endpoints::Create+ include Utilities::Endpoints::V3Deductions+ include Utilities::Endpoints::V3PresentSingle- # As this endpoint receives a list of file links to create, it calls the- # create service multiple times, one time for each file link to create. The- # call is done by calling the `super` method. Results are aggregated in- # global_result using the `add_dependent!` method.- def process(request, params_elements)- global_result = ServiceResult.success+ # As this endpoint receives a list of file links to create, it calls the+ # create service multiple times, one time for each file link to create. The+ # call is done by calling the `super` method. Results are aggregated in+ # global_result using the `add_dependent!` method.+ def process(request, params_elements)+ global_result = ServiceResult.success- Storages::FileLink.transaction do- params_elements.each do |params|- # call the default API::Utilities::Endpoints::Create#process- # implementation for each of the params_element array- one_result = super(request, params)- # merge service result in one- global_result.add_dependent!(one_result)- end+ ::Storages::FileLink.transaction do+ params_elements.each do |params|+ # call the default API::Utilities::Endpoints::Create#process+ # implementation for each of the params_element array+ one_result = super(request, params)+ # merge service result in one+ global_result.add_dependent!(one_result)+ end- # rollback records created if an error occurred (validation failed)- raise ActiveRecord::Rollback if global_result.failure?- end+ # rollback records created if an error occurred (validation failed)+ raise ActiveRecord::Rollback if global_result.failure?+ end- global_result- end+ global_result+ end- def present_success(request, service_call)- file_links = service_call.all_results.map do |file_link|- file_link.origin_status = :view_allowed- file_link- end+ def present_success(request, service_call)+ id_status_map = {}- render_representer.create(- file_links,- self_link: self_link(request),- current_user: request.current_user- )- end+ service_call.all_results.each do |file_link|+ id_status_map[file_link.id] = "view_allowed"+ end++ render_representer.create(+ JoinOriginStatusToFileLinksRelation.create(id_status_map),+ self_link: self_link(request),+ current_user: request.current_user+ )+ end- private+ private- def self_link(_request)- "#{::API::V3::URN_PREFIX}file_links:no_link_provided"+ def self_link(_request)+ "#{URN_PREFIX}file_links:no_link_provided"+ end+ end+ endendenddiff --git a/modules/storages/lib/api/v3/file_links/join_origin_status_to_file_links_relation.rb b/modules/storages/lib/api/v3/file_links/join_origin_status_to_file_links_relation.rbnew file mode 100644index 00000000000..b26b4348778--- /dev/null+++ b/modules/storages/lib/api/v3/file_links/join_origin_status_to_file_links_relation.rb@@ -0,0 +1,31 @@+# frozen_string_literal: true++module API+ module V3+ module FileLinks+ class JoinOriginStatusToFileLinksRelation+ # @param [Hash] id_status_map A hash mapping file link IDs to their origin status+ # in the format { 137: "view_allowed", 142: "error" }+ def self.create(id_status_map)+ sanitized_sql = ActiveRecord::Base.send(+ :sanitize_sql_array,+ [origin_status_join(id_status_map.size), *id_status_map.flatten]+ )++ ::Storages::FileLink.where(id: id_status_map.keys)+ .order(:id)+ .joins(sanitized_sql)+ .select("file_links.*, origin_status.status AS origin_status")+ end++ def self.origin_status_join(value_count)+ placeholders = Array.new(value_count).map { "(?,?)" }.join(",")++ <<-SQL.squish+ LEFT JOIN (VALUES #{placeholders}) AS origin_status (id,status) ON origin_status.id = file_links.id+ SQL+ end+ end+ end+ end+enddiff --git a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rbindex 0ac11d3f4a3..efdae12785f 100644--- a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb+++ b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb@@ -28,74 +28,78 @@# See COPYRIGHT and LICENSE files for more details.#++-class API::V3::FileLinks::WorkPackagesFileLinksAPI < API::OpenProjectAPI- helpers do- def sync_and_convert_relation(file_links)- sync_result = ::Storages::FileLinkSyncService- .new(user: current_user)- .call(file_links)- .result+module API+ module V3+ module FileLinks+ class WorkPackagesFileLinksAPI < API::OpenProjectAPI+ helpers do+ def sync_and_convert_relation(file_links)+ return ::Storages::FileLink.none if file_links.empty?- value_list = sync_result- .map { |file_link| "(#{file_link.id},'#{file_link.origin_status}')" }- .join(",")+ sync_result = ::Storages::FileLinkSyncService+ .new(user: current_user)+ .call(file_links)+ .result- origin_status_attribute = <<-SQL.squish- LEFT JOIN (VALUES #{value_list}) AS origin_status (id,status) ON origin_status.id = file_links.id- SQL+ id_status_map = {}- ::Storages::FileLink.where(id: sync_result.map(&:id))- .joins(origin_status_attribute)- .select("file_links.*, origin_status.status AS origin_status")- end- end+ sync_result.each do |file_link|+ id_status_map[file_link.id] = file_link.origin_status.to_s+ end- resources :file_links do- get do- query = ParamsToQueryService- .new(::Storages::Storage,- current_user,- query_class: ::Queries::Storages::FileLinks::FileLinkQuery)- .call(params)+ JoinOriginStatusToFileLinksRelation.create(id_status_map)+ end+ end- unless query.valid?- message = I18n.t("api_v3.errors.missing_or_malformed_parameter", parameter: "filters")- raise ::API::Errors::InvalidQuery.new(message)- end+ resources :file_links do+ get do+ query = ParamsToQueryService+ .new(::Storages::Storage,+ current_user,+ query_class: ::Queries::Storages::FileLinks::FileLinkQuery)+ .call(params)- relation = if current_user.allowed_in_project?(:view_file_links, @work_package.project)- file_links = query.results.where(container_id: @work_package.id,- container_type: "WorkPackage",- storage: @work_package.project.storages)+ unless query.valid?+ message = I18n.t("api_v3.errors.missing_or_malformed_parameter", parameter: "filters")+ raise ::API::Errors::InvalidQuery.new(message)+ end- if params[:pageSize] == "0"- file_links- else- sync_and_convert_relation(file_links)- end- else- ::Storages::FileLink.none- end+ relation = if current_user.allowed_in_project?(:view_file_links, @work_package.project)+ file_links = query.results.where(container_id: @work_package.id,+ container_type: "WorkPackage",+ storage: @work_package.project.storages)- ::API::V3::FileLinks::FileLinkCollectionRepresenter.new(- relation,- per_page: params[:pageSize],- self_link: api_v3_paths.file_links(@work_package.id),- current_user:- )- end+ if params[:pageSize] == "0"+ file_links+ else+ sync_and_convert_relation(file_links)+ end+ else+ ::Storages::FileLink.none+ end- post &::API::V3::FileLinks::WorkPackagesFileLinksCreateEndpoint- .new(- model: ::Storages::FileLink,- parse_service: Storages::Peripherals::ParseCreateParamsService,- render_representer: ::API::V3::FileLinks::FileLinkCollectionRepresenter,- params_modifier: ->(params) do- params[:container_id] = work_package.id- params[:container_type] = work_package.class.name- params- end+ FileLinkCollectionRepresenter.new(+ relation,+ per_page: params[:pageSize],+ self_link: api_v3_paths.file_links(@work_package.id),+ current_user:)- .mount+ end++ post &WorkPackagesFileLinksCreateEndpoint+ .new(+ model: ::Storages::FileLink,+ parse_service: ::Storages::Peripherals::ParseCreateParamsService,+ render_representer: FileLinkCollectionRepresenter,+ params_modifier: ->(params) do+ params[:container_id] = work_package.id+ params[:container_type] = work_package.class.name+ params+ end+ )+ .mount+ end+ end+ endendenddiff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rbindex f3de4f0b1e6..057233a04bb 100644--- a/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rb+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rb@@ -146,7 +146,7 @@ RSpec.describe "API v3 file links resource" do) doexpect(Storages::FileLink.count).to eq 2Storages::FileLink.find_each.with_index do |file_link, i|- unset_keys = %w[container_id container_type]+ unset_keys = %w[container_id container_type origin_status]set_keys = (file_link.attributes.keys - unset_keys)set_keys.each do |key|expect(file_link.attributes[key]).not_to(@@ -159,9 +159,8 @@ RSpec.describe "API v3 file links resource" doendend- expect(response.body).to be_json_eql(- "urn:openproject-org:api:v3:file_links:no_link_provided".to_json- ).at_path("_links/self/href")+ self_href = "urn:openproject-org:api:v3:file_links:no_link_provided?offset=1&pageSize=30".to_json+ expect(response.body).to be_json_eql(self_href).at_path("_links/self/href")endendend@@ -313,14 +312,20 @@ RSpec.describe "API v3 file links resource" do) doexpect(Storages::FileLink.count).to eq 2Storages::FileLink.find_each.with_index do |file_link, i|- file_link.attributes.each do |(key, value)|- # check nil values to ensure the :file_link_element factory is accurate- expect(value).not_to be_nil,- "expected attribute #{key.inspect} of FileLink ##{i + 1} to be set.\ngot nil."+ unset_keys = %w[origin_status]+ set_keys = (file_link.attributes.keys - unset_keys)+ set_keys.each do |key|+ expect(file_link.attributes[key]).not_to(+ be_nil,+ "expected attribute #{key.inspect} of FileLink ##{i + 1} to be set.\ngot nil."+ )+ end+ unset_keys.each do |key|+ expect(file_link.attributes[key]).to be_nilendend- expect(response.body).to be_json_eql(path.to_json).at_path("_links/self/href")+ expect(response.body).to be_json_eql("#{path}?offset=1&pageSize=30".to_json).at_path("_links/self/href")endend@@ -391,23 +396,18 @@ RSpec.describe "API v3 file links resource" do]end- it_behaves_like "API V3 collection response", 3, 3, "FileLink" do- let(:elements) { Storages::FileLink.order(id: :asc) }+ it_behaves_like "API V3 collection response", 1, 1, "FileLink" do+ let(:elements) { [Storages::FileLink.first] }let(:expected_status_code) { 201 }end- it(- "creates only one FileLink for all duplicates and " \- "uses metadata from the first item and " \- "replies with as many embedded elements as in the request, all identical"- ) do+ it "creates only one FileLink for all duplicates and uses metadata from the first item" doexpect(Storages::FileLink.count).to eq 1expect(Storages::FileLink.first.origin_name).to eq "first name"replied_elements = JSON.parse(last_response.body).dig("_embedded", "elements")- expect(replied_elements.count).to eq(embedded_elements.count)- expect(replied_elements[1..]).to all(eq(replied_elements.first))+ expect(replied_elements.count).to eq(1)endenddiff --git a/modules/storages/spec/requests/api/v3/file_links/mixed_case_file_links_integration_spec.rb b/modules/storages/spec/requests/api/v3/file_links/mixed_case_file_links_integration_spec.rbindex 4e3fddf3965..a02c51569ff 100644--- a/modules/storages/spec/requests/api/v3/file_links/mixed_case_file_links_integration_spec.rb+++ b/modules/storages/spec/requests/api/v3/file_links/mixed_case_file_links_integration_spec.rb@@ -171,13 +171,14 @@ RSpec.describe "API v3 file links resource" do# total, count, element_type, collection_type = 'Collection'it_behaves_like "API V3 collection response", 6, 6, "FileLink", "Collection" dolet(:elements) do+ # ordered by id[- file_link_timeout_happy,- file_link_error_happy,- file_link_unauth_happy,- file_link_deleted,+ file_link_happy,file_link_other_user,- file_link_happy+ file_link_deleted,+ file_link_unauth_happy,+ file_link_error_happy,+ file_link_timeout_happy]endenddiff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rbindex fe62052c5c9..ad8e3533b04 100644--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb@@ -1267,7 +1267,8 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter doendend- describe "fileLinks" do+ describe "fileLinks",+ skip: "test setup broken - remove embedding with https://community.openproject.org/wp/59468" dolet(:storage) { build_stubbed(:nextcloud_storage) }let(:file_link) { build_stubbed(:file_link, storage:, container: work_package) }