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 derive a field set to nil in request

Christophe Bliarde68eb424d1df
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index a6a4faa662b..00d048005a4 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -73,32 +73,32 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
# if one is nil and one is set, which should trigger the computation of the
# unchanged third one. Do it in the order :duration, :due_date, :start_date
# and return the first one that matches.
- if unchanged?(:duration) && both_set?(:start_date, :due_date)
+ if attribute_not_set_in_params?(:duration) && both_present?(:start_date, :due_date)
:duration
- elsif unchanged?(:due_date) && both_set?(:start_date, :duration)
+ elsif attribute_not_set_in_params?(:due_date) && both_present?(:start_date, :duration)
:due_date
- elsif unchanged?(:start_date) && both_set?(:due_date, :duration)
+ elsif attribute_not_set_in_params?(:start_date) && both_present?(:due_date, :duration)
:start_date
- elsif unchanged?(:duration) && only_one_is_nil?(:start_date, :due_date)
+ elsif attribute_not_set_in_params?(:duration) && only_one_present?(:start_date, :due_date)
:duration
- elsif unchanged?(:due_date) && only_one_is_nil?(:start_date, :duration)
+ elsif attribute_not_set_in_params?(:due_date) && only_one_present?(:start_date, :duration)
:due_date
- elsif unchanged?(:start_date) && only_one_is_nil?(:due_date, :duration)
+ elsif attribute_not_set_in_params?(:start_date) && only_one_present?(:due_date, :duration)
:start_date
end
end
# rubocop:enable Metrics/PerceivedComplexity, Lint/DuplicateBranch
- def unchanged?(field)
- !work_package.send("#{field}_changed?")
+ def attribute_not_set_in_params?(field)
+ !params.has_key?(field)
end
- def both_set?(*fields)
- work_package.values_at(*fields).count(&:nil?) == 0
+ def both_present?(*fields)
+ work_package.values_at(*fields).all?(&:present?)
end
- def only_one_is_nil?(*fields)
- work_package.values_at(*fields).count(&:nil?) == 1
+ def only_one_present?(*fields)
+ work_package.values_at(*fields).count(&:present?) == 1
end
# rubocop:disable Metrics/AbcSize
diff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rb
index c0057be8ea9..d02cd82746c 100644
--- a/spec/services/work_packages/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/set_attributes_service_spec.rb
@@ -925,15 +925,13 @@ describe WorkPackages::SetAttributesService,
{ initial: %i[start_date due_date duration], set: %i[start_date], nilled: %i[due_date], expected: { nilify: :duration } },
{ initial: %i[start_date ], set: %i[start_date], nilled: %i[due_date], expected: {} },
{ initial: %i[ due_date ], set: %i[start_date], nilled: %i[due_date], expected: {} },
- # TODO: is below scenario below ok? Problem is we can't detect that the user supplied nil as there is no change in the model
- { initial: %i[ duration], set: %i[start_date], nilled: %i[due_date], expected: { change: :due_date } },
+ { initial: %i[ duration], set: %i[start_date], nilled: %i[due_date], expected: { nilify: :duration, same: :due_date } },
{ initial: %i[ ], set: %i[start_date], nilled: %i[due_date], expected: {} },
{ initial: %i[start_date due_date duration], set: %i[due_date], nilled: %i[start_date], expected: { nilify: :duration } },
{ initial: %i[start_date ], set: %i[due_date], nilled: %i[start_date], expected: {} },
{ initial: %i[ due_date ], set: %i[due_date], nilled: %i[start_date], expected: {} },
- # TODO: is below scenario below ok? Problem is we can't detect that the user supplied nil as there is no change in the model
- { initial: %i[ duration], set: %i[due_date], nilled: %i[start_date], expected: { change: :start_date } },
+ { initial: %i[ duration], set: %i[due_date], nilled: %i[start_date], expected: { nilify: :duration, same: :start_date } },
{ initial: %i[ ], set: %i[due_date], nilled: %i[start_date], expected: {} },
{ initial: %i[start_date due_date duration], nilled: %i[start_date due_date], expected: {} },
@@ -950,15 +948,13 @@ describe WorkPackages::SetAttributesService,
{ initial: %i[start_date due_date duration], set: %i[start_date], nilled: %i[duration], expected: { nilify: :due_date } },
{ initial: %i[start_date ], set: %i[start_date], nilled: %i[duration], expected: {} },
- # TODO: is below scenario below ok? Problem is we can't detect that the user supplied nil as there is no change in the model
- { initial: %i[ due_date ], set: %i[start_date], nilled: %i[duration], expected: { change: :duration } },
+ { initial: %i[ due_date ], set: %i[start_date], nilled: %i[duration], expected: { nilify: :due_date, same: :duration } },
{ initial: %i[ duration], set: %i[start_date], nilled: %i[duration], expected: {} },
{ initial: %i[ ], set: %i[start_date], nilled: %i[duration], expected: {} },
{ initial: %i[start_date due_date duration], set: %i[duration], nilled: %i[start_date], expected: { nilify: :due_date } },
{ initial: %i[start_date ], set: %i[duration], nilled: %i[start_date], expected: {} },
- # TODO: is below scenario below ok? Problem is we can't detect that the user supplied nil as there is no change in the model
- { initial: %i[ due_date ], set: %i[duration], nilled: %i[start_date], expected: { change: :start_date } },
+ { initial: %i[ due_date ], set: %i[duration], nilled: %i[start_date], expected: { nilify: :due_date, same: :start_date } },
{ initial: %i[ duration], set: %i[duration], nilled: %i[start_date], expected: {} },
{ initial: %i[ ], set: %i[duration], nilled: %i[start_date], expected: {} },
@@ -975,15 +971,13 @@ describe WorkPackages::SetAttributesService,
{ initial: %i[ ], set: %i[due_date duration], expected: { change: :start_date } },
{ initial: %i[start_date due_date duration], set: %i[due_date], nilled: %i[duration], expected: { nilify: :start_date } },
- # TODO: is below scenario below ok? Problem is we can't detect that the user supplied nil as there is no change in the model
- { initial: %i[start_date ], set: %i[due_date], nilled: %i[duration], expected: { change: :duration } },
+ { initial: %i[start_date ], set: %i[due_date], nilled: %i[duration], expected: { nilify: :start_date, same: :duration } },
{ initial: %i[ due_date ], set: %i[due_date], nilled: %i[duration], expected: {} },
{ initial: %i[ duration], set: %i[due_date], nilled: %i[duration], expected: {} },
{ initial: %i[ ], set: %i[due_date], nilled: %i[duration], expected: {} },
{ initial: %i[start_date due_date duration], set: %i[duration], nilled: %i[due_date], expected: { nilify: :start_date } },
- # TODO: is below scenario below ok? Problem is we can't detect that the user supplied nil as there is no change in the model
- { initial: %i[start_date ], set: %i[duration], nilled: %i[due_date], expected: { change: :due_date } },
+ { initial: %i[start_date ], set: %i[duration], nilled: %i[due_date], expected: { nilify: :start_date, same: :due_date } },
{ initial: %i[ due_date ], set: %i[duration], nilled: %i[due_date], expected: {} },
{ initial: %i[ duration], set: %i[duration], nilled: %i[due_date], expected: {} },
{ initial: %i[ ], set: %i[duration], nilled: %i[due_date], expected: {} },
@@ -1012,8 +1006,9 @@ describe WorkPackages::SetAttributesService,
nilled = scenario[:nilled] || []
expected = scenario[:expected]
expected_change = expected[:change]
+ expected_same = expected[:same]
expected_nilify = expected[:nilify]
- unchanged = %i[start_date due_date duration] - set - nilled - expected.values
+ unchanged = %i[start_date due_date duration] - set - nilled - expected.values + [expected_same].compact
context_description = []
context_description << "with initial values for #{initial.inspect}" if initial.any?