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

nil non provided parameters on put

ulferts587e61c647ef
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 75016b09589..15185c5d631 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -201,7 +201,7 @@ module WorkPackages
end
def validate_status_exists
- errors.add :status, :does_not_exist if model.status&.is_a?(Status::InexistentStatus)
+ errors.add :status, :does_not_exist unless status_exists?
end
def validate_status_transition
@@ -282,7 +282,7 @@ module WorkPackages
end
def status_exists?
- model.status_id && model.status
+ model.status_id && model.status && !model.status.is_a?(Status::InexistentStatus)
end
def status_transition_exists?
diff --git a/app/models/type.rb b/app/models/type.rb
index 95f23fa014f..b923fea3d07 100644
--- a/app/models/type.rb
+++ b/app/models/type.rb
@@ -52,7 +52,7 @@ class ::Type < ActiveRecord::Base
association_foreign_key: 'custom_field_id'
belongs_to :color,
- class_name: 'Color',
+ class_name: 'Color',
foreign_key: 'color_id'
acts_as_list
diff --git a/modules/bcf/app/contracts/bcf/work_packages/update_contract.rb b/modules/bcf/app/contracts/bcf/work_packages/update_contract.rb
new file mode 100644
index 00000000000..ad0fb8f59a2
--- /dev/null
+++ b/modules/bcf/app/contracts/bcf/work_packages/update_contract.rb
@@ -0,0 +1,46 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 doc/COPYRIGHT.rdoc for more details.
+#++
+
+# The contract allows for setting the default status ignoring status transitions.
+# It is required so that put requests can set the status to the default status if the status property
+# is omitted.
+module Bcf::WorkPackages
+ class UpdateContract < ::WorkPackages::UpdateContract
+ def self.model
+ ::WorkPackage
+ end
+
+ private
+
+ def validate_status_transition
+ super if status_changed? && !model.status.is_default?
+ end
+ end
+end
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/update.rb b/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/update.rb
index dff9c2e14f5..ab8e83d7b12 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/update.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/update.rb
@@ -36,5 +36,9 @@ module Bcf::API::V2_1::Endpoints
render_representer
.new(call.result)
end
+
+ def postprocess_errors(call)
+ Bcf::API::V2_1::Errors::ErrorMapper.map(super)
+ end
end
end
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb
index 34e035fc1d8..94a8d032a4e 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/topics_api.rb
@@ -35,6 +35,35 @@ module Bcf::API::V2_1
def topics
Bcf::Issue.of_project(@project)
end
+
+ def transform_attributes(attributes)
+ wp_attributes = Bcf::Issues::TransformAttributesService
+ .new(@project)
+ .call(attributes)
+ .result
+
+ attributes
+ .slice(:stage,
+ :index,
+ :labels)
+ .merge(wp_attributes)
+ end
+
+ # In a put request, every non required and non provided
+ # parameter needs to be nilled. As we cannot nil type, status and priority
+ # as they are required for a work package we use the default values.
+ def default_put_params
+ {
+ index: nil,
+ assigned_to: nil,
+ description: nil,
+ due_date: nil,
+ subject: nil,
+ type: @project.types.default.first,
+ status: Status.default,
+ priority: IssuePriority.default
+ }
+ end
end
after_validation do
@@ -51,16 +80,7 @@ module Bcf::API::V2_1
.new(model: Bcf::Issue,
api_name: 'Topics',
params_modifier: ->(attributes) {
- wp_attributes = Bcf::Issues::TransformAttributesService
- .new(@project)
- .call(attributes)
- .result
-
- attributes
- .slice(:stage,
- :index,
- :labels)
- .merge(wp_attributes)
+ transform_attributes(attributes)
.merge(project: @project)
})
.mount
@@ -79,17 +99,8 @@ module Bcf::API::V2_1
.new(model: Bcf::Issue,
api_name: 'Topics',
params_modifier: ->(attributes) {
- # TODO: avoid code duplication with create
- wp_attributes = Bcf::Issues::TransformAttributesService
- .new(@project)
- .call(attributes)
- .result
-
- attributes
- .slice(:stage,
- :index,
- :labels)
- .merge(wp_attributes)
+ transform_attributes(attributes)
+ .reverse_merge(default_put_params)
})
.mount
diff --git a/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb b/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb
index 40a403ab4ff..bbc2e0c1b28 100644
--- a/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb
+++ b/modules/bcf/app/services/bcf/issues/transform_attributes_service.rb
@@ -77,7 +77,7 @@ module Bcf::Issues
return type if type.present?
- missing_type(project, type_name, attributes[:import_options] || {})
+ missing_type(type_name, attributes[:import_options] || {})
end
##
@@ -144,7 +144,7 @@ module Bcf::Issues
end
end
- def missing_type(project, type_name, import_options)
+ def missing_type(type_name, import_options)
if import_options[:unknown_types_action] == 'use_default'
project.types.default&.first
elsif import_options[:unknown_types_action] == 'chose' &&
diff --git a/modules/bcf/app/services/bcf/issues/update_service.rb b/modules/bcf/app/services/bcf/issues/update_service.rb
index 657221c3cb7..f256e58ebc9 100644
--- a/modules/bcf/app/services/bcf/issues/update_service.rb
+++ b/modules/bcf/app/services/bcf/issues/update_service.rb
@@ -34,7 +34,9 @@ module Bcf::Issues
def before_perform(params)
wp_call = ::WorkPackages::UpdateService
- .new(model: model.work_package, user: user)
+ .new(model: model.work_package,
+ user: user,
+ contract_class: Bcf::WorkPackages::UpdateContract)
.call(params)
if wp_call.success?
diff --git a/modules/bcf/spec/contracts/bcf/work_packages/update_contract_spec.rb b/modules/bcf/spec/contracts/bcf/work_packages/update_contract_spec.rb
new file mode 100644
index 00000000000..e260e825375
--- /dev/null
+++ b/modules/bcf/spec/contracts/bcf/work_packages/update_contract_spec.rb
@@ -0,0 +1,105 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
+#
+# 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 doc/COPYRIGHT.rdoc for more details.
+
+require 'spec_helper'
+
+describe Bcf::WorkPackages::UpdateContract do
+ let(:project) do
+ FactoryBot.build_stubbed(:project)
+ end
+ let(:work_package) do
+ FactoryBot.build_stubbed(:work_package,
+ project: project,
+ type: type)
+ end
+ let(:current_user) { FactoryBot.build_stubbed(:user) }
+ let(:type) { FactoryBot.build_stubbed(:type) }
+ let(:permissions) { %i[view_work_packages edit_work_packages] }
+
+ before do
+ allow(current_user)
+ .to receive(:allowed_to?) do |permission, context|
+ permissions.include?(permission) && context == project
+ end
+ end
+
+ subject(:contract) { described_class.new(work_package, current_user) }
+
+ describe 'status' do
+ let(:roles) { [FactoryBot.build_stubbed(:role)] }
+ let(:valid_transition_result) { true }
+ let(:new_status) { FactoryBot.build_stubbed(:status) }
+ let(:from_id) { work_package.status_id }
+ let(:to_id) { new_status.id }
+ let(:status_change) { work_package.status = new_status }
+
+ before do
+ allow(current_user)
+ .to receive(:roles)
+ .with(work_package.project)
+ .and_return(roles)
+
+ allow(type)
+ .to receive(:valid_transition?)
+ .with(from_id,
+ to_id,
+ roles)
+ .and_return(valid_transition_result)
+
+ status_change
+
+ contract.validate
+ end
+
+ context 'valid transition' do
+ it 'is valid' do
+ expect(subject.errors.symbols_for(:status_id))
+ .to be_empty
+ end
+ end
+
+ context 'invalid transition' do
+ let(:valid_transition_result) { false }
+
+ it 'is invalid' do
+ expect(subject.errors.symbols_for(:status_id))
+ .to match_array [:status_transition_invalid]
+ end
+ end
+
+ context 'invalid transition to default status' do
+ let(:new_status) { FactoryBot.build_stubbed(:status, is_default: true) }
+ let(:valid_transition_result) { false }
+
+ it 'is valid' do
+ expect(subject.errors.symbols_for(:status_id))
+ .to be_empty
+ end
+ end
+ end
+end
diff --git a/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb
index 8bd5d149953..1a64ba423b8 100644
--- a/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb
+++ b/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb
@@ -72,6 +72,7 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma
let(:work_package) do
FactoryBot.create(:work_package,
assigned_to: assignee,
+ due_date: Date.today,
project: project)
end
let(:bcf_issue) { FactoryBot.create(:bcf_issue, work_package: work_package) }
@@ -96,7 +97,7 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma
"creation_author": work_package.author.mail,
"creation_date": work_package.created_at.iso8601,
"description": work_package.description,
- "due_date": nil,
+ "due_date": work_package.due_date.iso8601,
"guid": bcf_issue.uuid,
"index": bcf_issue.index,
"labels": bcf_issue.labels,
@@ -145,7 +146,7 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma
"creation_author": work_package.author.mail,
"creation_date": work_package.created_at.iso8601,
"description": work_package.description,
- "due_date": nil,
+ "due_date": work_package.due_date.iso8601,
"guid": bcf_issue.uuid,
"index": bcf_issue.index,
"labels": bcf_issue.labels,
@@ -467,10 +468,9 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma
FactoryBot.create(:default_status)
end
let!(:default_type) do
- FactoryBot.create(:type, is_default: true)
- end
- let!(:standard_type) do
- FactoryBot.create(:type_standard, name: 'Standard type')
+ FactoryBot.create(:type, is_default: true).tap do |t|
+ project.types << t
+ end
end
let!(:priority) do
FactoryBot.create(:priority)
@@ -525,5 +525,55 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma
}
end
end
+
+ context 'with only the minimally required property (title)' do
+ let(:new_title) { 'New title' }
+ let(:params) do
+ {
+ title: new_title
+ }
+ end
+
+ it_behaves_like 'bcf api successful response' do
+ let(:expected_body) do
+ reloaded_work_package = WorkPackage.find(work_package.id)
+
+ {
+ guid: bcf_issue&.uuid,
+ topic_type: default_type.name,
+ topic_status: default_status.name,
+ priority: default_priority.name,
+ title: new_title,
+ labels: [],
+ index: nil,
+ reference_links: [
+ api_v3_paths.work_package(work_package&.id)
+ ],
+ assigned_to: nil,
+ due_date: nil,
+ stage: nil,
+ creation_author: work_package.author.mail,
+ creation_date: work_package&.created_at&.iso8601,
+ modified_author: edit_member_user.mail,
+ modified_date: reloaded_work_package&.updated_at&.iso8601,
+ description: nil
+ }
+ end
+ end
+ end
+
+ context 'with the title set to null' do
+ let(:params) do
+ {
+ title: nil
+ }
+ end
+
+ it_behaves_like 'bcf api unprocessable response' do
+ let(:message) do
+ "Title can't be blank."
+ end
+ end
+ end
end
end
diff --git a/spec/contracts/work_packages/update_contract_spec.rb b/spec/contracts/work_packages/update_contract_spec.rb
index feb9aa932f3..f75a9ddf941 100644
--- a/spec/contracts/work_packages/update_contract_spec.rb
+++ b/spec/contracts/work_packages/update_contract_spec.rb
@@ -42,7 +42,6 @@ describe WorkPackages::UpdateContract do
FactoryBot.build_stubbed(:work_package,
project: project,
type: type).tap do |wp|
-
wp_scope = double('wp scope')
allow(WorkPackage)