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
add checks on _embedded/elements when creating file links through api
Christophe Bliard8814088c9d23
diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 0772808f9eb..755d063f5b8 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -3063,6 +3063,7 @@ en:select: "The requested select of %{invalid} is not supported. Supported selects are %{supported}."invalid_user_status_transition: "The current user account status does not allow this operation."missing_content_type: "not specified"+ missing_property: "Missing property '%{property}'."missing_request_body: "There was no request body."missing_or_malformed_parameter: "The query parameter '%{parameter}' is missing or malformed."multipart_body_error: "The request body did not contain the expected multipart parts."diff --git a/docs/api/apiv3/tags/basic_objects.yml b/docs/api/apiv3/tags/basic_objects.ymlindex 924b9e75133..d9815f95435 100644--- a/docs/api/apiv3/tags/basic_objects.yml+++ b/docs/api/apiv3/tags/basic_objects.yml@@ -174,6 +174,9 @@ description: |-* `urn:openproject-org:api:v3:errors:PropertyFormatError` (**HTTP 422**)A property value was provided in a format that the server does not understand or accept+ * `urn:openproject-org:api:v3:errors:PropertyMissingError` (**HTTP 422**)+ The request is syntactically correct, but is missing a property to perform the requested action+* `urn:openproject-org:api:v3:errors:PropertyValueNotAvailableAnymore` (**HTTP 422**)An unchanged property needs to be changed, because other changes to the resource make it unavailablediff --git a/lib/api/errors/property_missing_error.rb b/lib/api/errors/property_missing_error.rbnew file mode 100644index 00000000000..fdc59b60bd1--- /dev/null+++ b/lib/api/errors/property_missing_error.rb@@ -0,0 +1,49 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-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 Errors+ class PropertyMissingError < ErrorBase+ identifier 'PropertyMissingError'+ code 422++ attr_accessor :property++ def initialize(property)+ self.property = property++ message = I18n.t('api_v3.errors.missing_property', property: property)+ super message+ end++ def details+ { attribute: property }+ end+ end+ end+enddiff --git a/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb b/modules/storages/lib/api/v3/file_links/parse_create_params_service.rbindex fc3d1db59ab..3856affa88d 100644--- a/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb+++ b/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb@@ -36,11 +36,20 @@ module API::V3::FileLinksenddef parse_elements(request_body)- request_body.dig("_embedded", "elements").map do |params|+ request_body.dig("_embedded", "elements")+ .tap { ensure_valid_elements(_1) }+ .map do |params|API::V3::FileLinks::FileLinkRepresenter.new(Hashie::Mash.new, current_user: current_user).from_hash(params)endend++ private++ def ensure_valid_elements(elements)+ raise API::Errors::PropertyMissingError.new('_embedded/elements') if elements.blank?+ raise API::Errors::PropertyFormatError.new('_embedded/elements', 'Array', elements.class.name) unless elements.is_a?(Array)+ endendenddiff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rbindex 1af682ea68c..9be98e9c874 100644--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb@@ -138,20 +138,50 @@ describe 'API v3 file links resource', type: :request dolet(:expected_status_code) { 201 }end- context 'if the request body contains an invalid storage host' do- let(:storage_url1) { 'https://invalid.host.org/' }+ context 'when storage host is invalid' do+ context 'when unknown host' do+ let(:storage_url1) { 'https://invalid.host.org/' }- it_behaves_like 'constraint violation' do- let(:message) { 'Storage is invalid' }+ it_behaves_like 'constraint violation' do+ let(:message) { 'Storage is invalid' }+ end+ end++ context 'when not linked to the project of the work package' do+ let(:storage_url1) { another_storage.host }++ it_behaves_like 'constraint violation' do+ let(:message) { 'Storage is not linked to project' }+ endendend- context 'if the request body contains a storage host that is not linked to the project of the work package' do- let(:storage_url1) { another_storage.host }+ context 'when no _embedded/elements in given json' do+ let(:params) do+ {}+ end++ it_behaves_like 'missing property', I18n.t('api_v3.errors.missing_property', property: '_embedded/elements')+ end- it_behaves_like 'constraint violation' do- let(:message) { 'Storage is not linked to project' }+ context 'when _embedded/elements is empty' do+ let(:params) do+ { _embedded: { elements: [] } }end++ it_behaves_like 'missing property', I18n.t('api_v3.errors.missing_property', property: '_embedded/elements')+ end++ context 'when _embedded/elements is not an array' do+ let(:params) do+ { _embedded: { elements: 42 } }+ end++ it_behaves_like 'format error',+ I18n.t('api_v3.errors.invalid_format',+ property: '_embedded/elements',+ expected_format: 'Array',+ actual: 'Integer')endenddiff --git a/spec/requests/api/v3/support/response_examples.rb b/spec/requests/api/v3/support/response_examples.rbindex 65ef5d07bfc..56300077735 100644--- a/spec/requests/api/v3/support/response_examples.rb+++ b/spec/requests/api/v3/support/response_examples.rb@@ -151,6 +151,13 @@ shared_examples_for 'format error' do |message|messageend+shared_examples_for 'missing property' do |message|+ it_behaves_like 'error response',+ 422,+ 'PropertyMissingError',+ message+end+shared_examples_for 'read-only violation' do |attribute, model, attribute_message = nil|describe 'details' dosubject { JSON.parse(last_response.body)['_embedded']['details'] }