Skip to content

Commit

Permalink
Don't raise on every unexpected param when using expect
Browse files Browse the repository at this point in the history
Not quite perfect yet, but starts down the right path.
  • Loading branch information
martinemde committed Aug 4, 2024
1 parent 011355c commit 6bd3e34
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions actionpack/lib/action_controller/metal/strong_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,9 @@ def require(key)
# object # => #<ActionController::Parameters {"pie"=>"pumpkin"} permitted: true>
#
def expect(*filters)
params = apply_filters(filters)
keys = filters.map {|f| f.respond_to?(:keys) ? f.keys : f }.flatten(1)
values = permit(*filters).require(keys)
values = params.permit!.require(keys)
values.size == 1 ? values.first : values
end

Expand Down Expand Up @@ -807,16 +808,7 @@ def expect(*filters)
# params.permit(person: { '0': [:email], '1': [:phone]}).to_h
# # => {"person"=>{"0"=>{"email"=>"[email protected]"}, "1"=>{"phone"=>"555-6789"}}}
def permit(*filters)
params = self.class.new

filters.flatten.each do |filter|
case filter
when Symbol, String
permitted_scalar_filter(params, filter)
when Hash
hash_filter(params, filter)
end
end
params = apply_filters(filters)

unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters

Expand Down Expand Up @@ -1262,10 +1254,25 @@ def each_element(object, filter, &block)
end
end

def unpermitted_parameters!(params)
def apply_filters(filters)
params = self.class.new

filters.flatten.each do |filter|
case filter
when Symbol, String
permitted_scalar_filter(params, filter)
when Hash
hash_filter(params, filter)
end
end

params
end

def unpermitted_parameters!(params, action = self.class.action_on_unpermitted_parameters)
unpermitted_keys = unpermitted_keys(params)
if unpermitted_keys.any?
case self.class.action_on_unpermitted_parameters
case action
when :log
name = "unpermitted_parameters.action_controller"
ActiveSupport::Notifications.instrument(name, keys: unpermitted_keys, context: @logging_context)
Expand Down

0 comments on commit 6bd3e34

Please sign in to comment.