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

ensure duration to be integer

ulfertsf5fc2b9c5962
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index db319180f8a..f3742638226 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -131,9 +131,13 @@ module WorkPackages
validate :validate_assigned_to_exists
validates :duration,
- comparison: { greater_than: 0 },
+ # only_integer: true, cannot be used as that will not compare with the value
+ # before the type cast. So even a float value will pass the validation as it is silently
+ # floored.
+ numericality: { greater_than: 0 },
allow_nil: true
+ validate :validate_duration_integer
validate :validate_duration_matches_dates
def initialize(work_package, user, options: {})
@@ -313,6 +317,10 @@ module WorkPackages
end
end
+ def validate_duration_integer
+ errors.add :duration, :only_integer if model.duration_before_type_cast != model.duration
+ end
+
def validate_duration_matches_dates
return unless calculated_duration && model.duration
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index aba45789a5d..58bd48025d6 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -493,6 +493,14 @@ describe WorkPackages::BaseContract do
it_behaves_like 'contract is invalid', duration: :greater_than
end
+ context 'when setting the duration to a floating point' do
+ before do
+ work_package.duration = 4.5
+ end
+
+ it_behaves_like 'contract is invalid', duration: :only_integer
+ end
+
context 'when setting the duration to a negative value' do
before do
work_package.duration = -5