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

Do not swallow error if work is not a number

Christophe Bliard476e5d6f751a
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 4d4e14a2d89..0d38d5fd793 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -332,8 +332,14 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributes
end
def round_progress_values
- work_package.estimated_hours = work_package.estimated_hours&.round(2)
- work_package.remaining_hours = work_package.remaining_hours&.round(2)
+ rounded = work_package.estimated_hours&.round(2)
+ if rounded != work_package.estimated_hours
+ work_package.estimated_hours = rounded
+ end
+ rounded = work_package.remaining_hours&.round(2)
+ if rounded != work_package.remaining_hours
+ work_package.remaining_hours = rounded
+ end
end
def update_remaining_hours_from_percent_complete
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index 5614e21e3d5..035e248b9d9 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -569,6 +569,19 @@ RSpec.describe WorkPackages::SetAttributesService,
"and remaining work is updated and rounded to 2 decimals"
end
+ context "when work is set to a string" do
+ let(:call_attributes) { { estimated_hours: "I am a string" } }
+ let(:expected_attributes) { { estimated_hours: 0.0, remaining_hours: 0.0 } }
+
+ it "keeps the original string value in the _before_type_cast method " \
+ "so that validation can detect it is invalid" do
+ allow(work_package).to receive(:save)
+ instance.call(call_attributes)
+
+ expect(work_package.estimated_hours_before_type_cast).to eq("I am a string")
+ end
+ end
+
context "when work and remaining work are set" do
let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: 0 } }
let(:expected_attributes) { call_attributes.merge(done_ratio: 100) }