diff --git a/app/models/application_model/can_cleanup_param.rb b/app/models/application_model/can_cleanup_param.rb index c2f3286fd..751399fc8 100644 --- a/app/models/application_model/can_cleanup_param.rb +++ b/app/models/application_model/can_cleanup_param.rb @@ -47,31 +47,27 @@ returns data.delete('id') end + # get associations by id + attribute_associations = {} + reflect_on_all_associations.map do |assoc| + class_name = assoc.options[:class_name] + next if !class_name + + attribute_associations["#{assoc.name}_id"] = assoc + end + # only use object attributes clean_params = ActiveSupport::HashWithIndifferentAccess.new new.attributes.each_key do |attribute| next if !data.key?(attribute) - invalid = false - # check reference records, referenced by _id attributes - reflect_on_all_associations.map do |assoc| - class_name = assoc.options[:class_name] - next if !class_name + if attribute_associations[attribute].present? && data[attribute].present? && !attribute_associations[attribute].klass.lookup(id: data[attribute]) + raise Exceptions::UnprocessableEntity, "Invalid value for param '#{attribute}': #{data[attribute].inspect}" if exceptions - name = "#{assoc.name}_id" - next if !data.key?(name) - next if data[name].blank? - next if assoc.klass.lookup(id: data[name]) - - raise Exceptions::UnprocessableEntity, "Invalid value for param '#{name}': #{data[name].inspect}" if exceptions - - invalid = true - break + next end - next if invalid - clean_params[attribute] = data[attribute] end diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index e1a652edd..1725d4223 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -980,6 +980,12 @@ RSpec.describe Ticket, type: :model do end end end + + describe '#param_cleanup' do + it 'does only remove parameters which are invalid and not the complete params hash if one element is invalid (#3743)' do + expect(described_class.param_cleanup({ state_id: 3, customer_id: 'guess:1234' }, true, false, false)).to eq({ 'state_id' => 3 }) + end + end end describe 'Attributes:' do